0

I'm doing my homework,that i need to use quick sort to sort string array,and the element in array combing number and string. for example

String s[];

s[0]="172,19,Nina";
s[1]="178,18,Apple";
s[2]="178,18,Alex";

So after sorted, it should be

s[0]=172,19,Nina
s[1]=178,18,Alex
s[2]=178,18,Apple

Im thinking should i split all the Strings first into number and string, and then sort 172,178,178, and then sort 19 18 18, and at the end sort Nina Apple Alex??

what is the best way to do this?

2
  • 2
    What is the proper sort order of "98,19,Albert" and "120,19,Albert" in your exercise? Commented May 27, 2012 at 11:39
  • Isn't using compareTo() method a solution here? Asking this question to people who see this question,not to the OP. Commented May 27, 2012 at 11:43

2 Answers 2

3

If all your numbers have the same number of characters, lexicographic order is the same as numeric order, so you might just compare your Strings directly.

Else, you should split the strings and transform them into proper objects which implement the Comparable interface:

public class Record implements Comparable<Record> {
    private int firstNumber;
    private int secondNumber;
    private String name;

    ...

    @Override
    public int compareTo(Record r) {
        int result = Integer.valueOf(firstNumber).compareTo(Integer.valueOf(r.firstNumber);
        if (result != 0) {
            result = Integer.valueOf(secondNumber).compareTo(Integer.valueOf(r.secondNumber);
        }
        if (result != 0) {
            result = name.compareTo(r.name);
        }
        return result;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Yes, you are right: you need to separate the combined string into its elements and sort the array based on these separated elements. Note that you do not need to sort the full array based on the first number, then the second, etc., but provide a comparison logic based on them. This is called lexical ordering

Basically, when you need to decide whether an element is less then another, you have the following logic (pseudocode):

if elem1's first number < elem2's first number
then
  return elem1 less than elem2
else if elem1's first number > elem2's first number
then
  return elem1 greater than elem2
// from here on: elem1's first number == elem2's first number
else if elem1's second number < elem2's second number
then
  return elem1 less then elem2
else if elem1's second number > elem2's second number
then
  return elem1 greater than elem2
// from here on: elem1's second number == elem2's second number
else if elem1's third string < elem2's third string
then 
  return elem1 less then elem2
else if elem1's third string > elem2's third string
then
  return elem1 greater than elem2
else // everything is the same
  return elem1 equal elem2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.