• 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 a list

 
Greenhorn
Posts: 11
  • 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 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
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic