The Artima Developer Community
Sponsored Link

Java Buzz Forum
How to sort Object in Java - Comparator and Comparable Example

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Javin Paul

Posts: 1090
Nickname: javinpaul
Registered: Jan, 2012

Javin Paul is Java Programmer working on Finance domain.
How to sort Object in Java - Comparator and Comparable Example Posted: Oct 25, 2012 4:34 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Javin Paul.
Original Post: How to sort Object in Java - Comparator and Comparable Example
Feed Title: Java67
Feed URL: http://www.java67.com/feeds/posts/default?alt=rss
Feed Description: Java and technology tutorials, tips, questions for all programmers.
Latest Java Buzz Posts
Latest Java Buzz Posts by Javin Paul
Latest Posts From Java67

Advertisement


Java Object Sorting Example
How do you sort list of Objects in Java is one of the frequently asked coding questions in Java interviews and surprisingly not every Java programmers know How sorting of object happens in Java. Comparator and Comparable interface along with Collections.sort() method is used to sort list of object in Java. compare() and compareTo() method of Comparator and Comparable interface provides comparison logic needed for sorting objects. compareTo() method is used to provide Object's natural order sorting and compare() method is used to sort Object with any arbitrary field. Almost all value classes in Java library e.g. String, Integer, Double, BigDecimal implement compareTo() to specify there natural sorting order. While overriding compareTo method String is sorted lexicographically and Integers are sorted numerically. Just beware that it must in consistent with equals method i.e. two objects which are equal by equals method in Java, compareTo() method must return zero for them.  Any way,  sorting standard value Object is not a problem for many Java programmer but some of them really struggle when it comes to sort custom Objects or domain Objects. In this Java sorting tutorial we will create custom object and sort list of Object in ascending and descending order.
Read more »

Read: How to sort Object in Java - Comparator and Comparable Example

Topic: How to escape HTML Special characters in JSP and Java Previous Topic   Next Topic Topic: Spring Data: NoSQL No Problem

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use