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
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply 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://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: I am having trouble sorting an array of records that contains strings
 
Similar Threads
sorting
Sorting the best way
QuickSort Algorithm and Generics
jsp Iterate tag
Sorting with two fields