aspose file tools*
The moose likes Beginning Java and the fly likes I am having trouble sorting an array of records that contains strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "I am having trouble sorting an array of records that contains strings" Watch "I am having trouble sorting an array of records that contains strings" New topic
Author

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

J. Ward
Greenhorn

Joined: Apr 21, 2002
Posts: 1
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

Joined: Dec 10, 2001
Posts: 7023
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?


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: I am having trouble sorting an array of records that contains strings
 
Similar Threads
Sorting the best way
Sorting with two fields
jsp Iterate tag
sorting
QuickSort Algorithm and Generics