This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes Sorting a list Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Sorting a list " Watch "Sorting a list " New topic
Author

Sorting a list

Mohamed Amri
Greenhorn

Joined: Feb 17, 2006
Posts: 11
I have a list of Student type objects. I want the elements of this list to be sorted according to the Student name.

How should I deal with that ?
Thanks
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37884
    
  22
Create a Comparator object which returns differences between names (possibly even divided into first name, last name).
Assuming you aren't supposed to use the Collections class methods, you will have to implements one of the standard searching algorithms, taking your List and the Comparator as parameters. There have been several posts about how to find sorting algorithms this last week; have a search through the beginner's forum.

Probably best to sort the way Collections#sort(java.util.List, java.util.Comparator) does, creating an array and sorting the array.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sorting a list
 
Similar Threads
DataStructure help
Loop through the list of objects to get attribute.
Are there structs in Java? Are they even called structs?
convert array to Arraylist
[imp] how can we sort the objects in Collections