GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes Sort Objects by Comparator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Sort Objects by Comparator" Watch "Sort Objects by Comparator" New topic
Author

Sort Objects by Comparator

nazeer hussain
Greenhorn

Joined: Feb 06, 2006
Posts: 21
Hi Guys

Can anyone knows the code to Sort the collection of objects, having two integer data types say a,b and assume the object is POJO which will not be implemented with any interface(Serializable/Comparator/..). Requirement is as follows.

Arraylist
- POJO
- int a = 100
- int b = 150
- POJO
- int a = 75
- int b = 125
-
5 items


list Ordered by asc based a values

list Ordered by asc based b values

using Comparator
-----------------------------

Thanks
Nazeer

:roll:
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi Nazeer,

Can you give a little code here, so that we could think of, what exactly the issue is?



Regards,
cmbhatt


cmbhatt
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707


Is this solution to your question?


Regards,
cmbhatt
[ April 16, 2007: Message edited by: Chandra Bhatt ]
Naveen K Garg
Ranch Hand

Joined: Nov 28, 2005
Posts: 105
Also look at following thread

Sorting

Regards
Naveen
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi ranchers,

interface Comparable is used when you have access to the class and you can implement it.

interface Comparator can be used, when you want to sort any class.

You set up a Comparator for any item you want to sort.
If you want to use generics, the name of the class to be sorted goes into the < >.

Here's an example, to compare the legs of animals.

Write your own comparators using that scheme (e.g. to sort the animals name or to sort your pojos).


Yours,
Bu.


all events occur in real time
nazeer hussain
Greenhorn

Joined: Feb 06, 2006
Posts: 21
Thank you guys for your reply. Got the solution.
 
GeeCON Prague 2014
 
subject: Sort Objects by Comparator