• 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

Sorting objects in ArrayList

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I just wanted to know on what basis are objects in ArrayList get Sorted when i sort an ArrayList using Collections.sort(arraylist);
this sort method internally uses Arrays.sort(array).But still on what basis are the objects sorted?


Thanks in advance.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when you tried do you get any compilation error ? if yes what is the error message?
 
sindhu sheela
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
no i did not get any compilation error.it was for my understanding and further implementation i wanted it.on what basis are the objects sorted. like i will attributes in the object as id,name,address etc.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Collections.sort uses the Comparable interface to sort the object in the list, so these objects must implement Comparable and implement its method. The only method of that interface is int compareTo(T o). That is where you decide which fields of your objects have to be compared.

Or, a Comparator can be used to compare the objects. In this case, Collections.sort(List<T> list, Comparator<? super T>) has to be used.
 
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

i wanted it.on what basis are the objects sorted. like i will attributes in the object as id,name,address etc.

Looking at your requirement either you can take id or name as a basis of sorting. Suppose you have some Student objects and you want to show list of students in ascending order of id/roll or you can present the list in alphabetical order in case of name. Check the java doc that how Collection.sort works and take a detailed study of Comparable and Comparator interfaces.
 
Ranch Hand
Posts: 448
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have posted a bog entry for your answer:

http://extreme-java.blogspot.com/2011/01/how-to-sort-arraylist-using-collections.html
 
sindhu sheela
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All the answer where quite informative and cleared my doubt .thanks a lot once again.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic