Hi im trying to write a program that sorts an array in alphabetical order and then into phone book order,

Eg input:
Jane Pascal 364756
Bob Smith 364758
Joe Bloggs 253647

The output should be:
Bloggs, Joe 253647
Pascal, Jane 364756
Smith, Bob 364758

The plan is to have each name (forename + surname) in different array's and the numbers in another and not sure how to sort them as above, any help would be great.

10 Years
Discussion Span
Last Post by titaniumdecoy

I would use the StringTokenizer to parse up the individual strings according to the spaces;

String foreName, surName, number;
StringTokenizer st = new StringTokenizer(str);
foreName = st.nextToken;
surName = st.nextToken;
number = st.nextToken;

then rearranging the strings

newStr = surName+", "+foreName+" "+number;

stick those into an array and then use the Arrays.sort


Thanks, I new there was an Array.sort method just wasnt sure how to use it, cheers!


An even better way to do this would be to create a Person class with the appropriate fields, and create a new class that implements Comparator for each desired sort order. See here.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.