1

I have this 2 dimensional string array.

2 10 BakerSarah D 
2 11 SmothersSally A 
2 12 SillySall C 
2 13 Viper B 
2 5 LouieChef B 
2 6 Lawson C  

Each column is string . Now I want to sort this on second column . i have tried this code

void sortarray(final int index){
        Arrays.sort(data, new Comparator<Object[]>(){
            @Override
            public int compare(Object[] o1,  Object[] o2) {
                String[] a = (String[])o1;
                String[] b = (String[])o1;
                return a[index].compareTo(b[index]);
            }
        });

    }   

but this is giving sort in

    2 10 BakerSarah D 
    2 11 SmothersSally A 
    2 12 SillySall C 
    2 13 Viper B 
    2 5 LouieChef B 
    2 6 Lawson C  

in order . Why so ?? How to change that to sort

2 5 LouieChef B 
2 6 Lawson C   
2 10 BakerSarah D 
2 11 SmothersSally A 
2 12 SillySall C 
2 13 Viper B 
1
  • you want integer sorting, but your value is still a string, you need to convert it to integer, and compare the integers Commented Aug 20, 2013 at 12:09

2 Answers 2

1

When the compare returns 0 (ie they are equal) then you need compare on another index. I have updated your code to have this new index - index2.

void sortarray(final int index, final int index2){
    Arrays.sort(data, new Comparator<Object[]>(){
        @Override
        public int compare(Object[] o1,  Object[] o2) {
            String[] a = (String[])o1;
            String[] b = (String[])o1;
            Integer i = a[index].compareTo(b[index]);
            if (i == 0) {
               return a[index2].compareTo(b[index2]);
            } 
            return i;
        }
    });

}   

I am assuming (perhaps incorrectly) that you want to sort my the first column and then by the second. If it is just the second then try what @x4rf41 says and do an Integer.valueOf to convert the string to an integer

Personally though I would create an object and implement Comparator on this so that you can sort in a more OO way.

Sign up to request clarification or add additional context in comments.

4 Comments

you need to convert the string to an integer, then the sorting will work
index2 is the second index you want to sort on. In this example it is the column containing 5,6,10,....
but that's index parameter itself
That really doesn't answer the question.
1

Strings have a natural, lexicographic order. Which means that "10" comes before "5". Integers have a natural, numeric order. So you should transform your strings into numbers and compare the numbers:

Arrays.sort(data, new Comparator<Object[]>(){
    @Override
    public int compare(Object[] o1,  Object[] o2) {
        String[] a = (String[])o1;
        String[] b = (String[])o1;
        if (index == 2) { // lexicographic order
            return a[index].compareTo(b[index]);
        }
        else { // numeric order
            int left = Integer.parseInt(a[index]);
            int right = Integer.parseInt(b[index]);
            return Integer.compare(left, right);
        }
    }
});

Note that this wouldn't happen if, instead of using a String[] to hold your information, you used a proper class, with fields of the appropriate type:

public class Row { // choose a better name
    private int field1; // choose a better name
    private int field1; // choose a better name
    private String name;

    // constructor and getters omitted
}

Java is an OO language. Use objects.

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.