• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Sort Objects by Comparator

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Is this solution to your question?


Regards,
cmbhatt
[ April 16, 2007: Message edited by: Chandra Bhatt ]
 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also look at following thread

Sorting

Regards
Naveen
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you guys for your reply. Got the solution.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic