• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sort Objects by Comparator

 
nazeer hussain
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nazeer,

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



Regards,
cmbhatt
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Is this solution to your question?


Regards,
cmbhatt
[ April 16, 2007: Message edited by: Chandra Bhatt ]
 
Naveen K Garg
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also look at following thread

Sorting

Regards
Naveen
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
nazeer hussain
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you guys for your reply. Got the solution.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic