This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

alphabetizing lists  RSS feed

 
Mario Bonassin
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have no idea how to do this one. I need a simple code that will take a listModel and alphabetize the items. I would like it if it could do it with the first letter and last letter, seperatly of course.
Any suggestions or advise would be great.
Mario
 
sever oon
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'll want to use java.util.Comparator and java.lang.Comparable. You can write a comparator to sort a java Collection in any way you see fit. You can also use the Comparator to instantiate sorted collections so you don't have to sort things all at once, they get inserted into their sorted positions at the time of insertion. Or, many classes already implement the Comparable interface so they have a "natural ordering". Strings have this capability already...

sev
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!