• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

I am having trouble sorting an array of records that contains strings

 
J. Ward
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was wondering how do I sort an array of records objects that contain string values. The value that I am trying to sort by is the last name. Also, I need to create a method that will sort the array in ascending or descending order by the last name of the objects created. Any help that can be provided concerning this matter will be greatly appreciated.
- J. Ward
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A couple of questions:
1. When you say that your array of records contains string values and you want to sort by last name, what do you mean exactly? Is this a possible component in the array:
"Dirk Schreckmann"
This is what I'd guess you mean.
2. Have you run across the The API Specification yet? Taking a look at the documentation for the class String, some useful methods might be compareTo(String), indexOf(int ch), substring(int), and/or substring(int, int).
If you were to use a Collection (and the Collections class) you could abstract yourself a bit from the nitty-gritty details of sorting, but String::compareTo(String) already removes one from this quite a bit.
Do you have any ideas on where you'd like to begin?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic