Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sorting a list

 
Mohamed Amri
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49776
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic