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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.