• 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 by Name

 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a list of user information as follows. I would like to sort the list of info in ascending order by their names. I would appreciate if someone can help me out on this. Thanks....

Now the List is like:

Paul Kam, 56, 80000

Clark Pam, 35, 75000

Mann Sam, 46, 78000


I want to sort them like:

Clark Pam, 35, 75000

Mann Sam, 46, 78000

Paul Kam, 56, 80000



 
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
What type of objects is in the List ? List<String[]> ? List<Person> ?
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Implement Comparable and create at compareTo method which compares whichever value you wish to sort by.

This example below would be if I just wanted to compare my toStrings for an object, you can do the same for any value though.
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What sort of objects? if it's List you can always use Collections.sort();
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

--> If the list of Users that you are trying to sort is a List of String instances, then, use:

java.util.Collections.sort(listOfUsers);// listOfUsers is the list which contains the data that you have provided.

List will be sorted in the desired order.

--> If the list is a collection of user-defined objects say "User" objects, then, use a Comparator to sort the data. i.e, implement java.util.Comparator interface. User POJO should implement java.util.Comparator interface and the method compare() declared in it.
then call:

java.util.Collections.sort(listOfUsers, new MyComparator());// listOfUsers is the list which contains the data that you have provided and MyComparator is the class which implements java.util.Comparator interface.

sample code to implement compare() method:

int compare(Object o1, Object o2){
User u1 = null;
User u2 = null;
if(o1 != null && o1 instanceof User)
u1 = (User) o1;
if(o2 != null && o2 instanceof User)
u2 = (User) o2;

return u1.getUserName().compareTo(u2.getUserName()); // null check the objects before this comparision
}

reply
    Bookmark Topic Watch Topic
  • New Topic