1

I know how to sort an array that has two columns:

Arrays.sort(myarray, new Comparator<String[]>() {
                     @Override
                     public int compare(String[] entry1, String[] entry2) {
                         String time1 = entry1[0];
                         String time2 = entry2[0];
                         return time2.compareTo(time1);
                     }
                });

This sorts the arrray by the first column. But what if I have more columns? E.g.

myarray[0][0]= +3620205252
myarray[0][1]= 32534
myarray[0][2]= Franco Nera
myarray[0][3]= 183
myarray[1][0]= +3658300234
myarray[1][1]= 4334
myarray[1][2]= Judy Moira
myarray[1][3]= 28

etc.. I want to sort this e.g. by the second column, or the fourth column... I can try to work this out by creating a new array[1st+3rd+4th column][2nd column] and then sort it with the above solution, then take the elements apart, but that is too circumstantial.

2
  • 1
    What's the problem? entry[0] is the first column, entry[1] is the second column, entry[2] is the third column and so on. Just compare entry1[3] with entry2[3] to compare the fourth column. You should put your data in a proper object instead of a String array though. Commented Oct 11, 2011 at 13:11
  • 2
    You should really use objects. The array should be an array of objects, each object having 4 properties (time, name, etc.).
    – JB Nizet
    Commented Oct 11, 2011 at 13:14

5 Answers 5

2

To sort by a different column just replace the [0] in these lines:

String time1 = entry1[0];
String time2 = entry2[0];

If you want to sort by multiple columns, you need to first compare by the highest order preference, e.g. :

String time1 = entry1[0];
String time2 = entry2[0];
int cmp = time2.compareTo(time1);

and then if the result is zero (i.e. they were equal) compare by the next highest preference:

if (cmp == 0) {
    String s1 = entry1[1];
    String s2 = entry2[1];
    cmp = s2.compareTo(s1);
}

return cmp;
1

It's the same thing as with two columns, but with more columns: you can sort on as many, or as few, columns as you want, as long as your comparator checks the columns you care about.

To sort by multiple columns you need to decide the comparison order and return the compareTo result as soon as you have unequal columns.

Tangentially, why would you use arrays for this? It's Java, might as well use like an object or something.

1
  • I tried this out, but my app force closed, and thought that this caused the close. Now i fixed this error and it works, thanks and to you all!
    – erdomester
    Commented Oct 11, 2011 at 17:41
1

You have to walk the two arrays until you find an index where the elements differ and the return the comparison of the strings at that index. It is basically the same idea that is used in comparing strings.

for(int i=0;i<Math.min(entry1.length,entry2.length);i++){
   String x=entry1[i], y=entry2[i];
   int diff=x.compareTo(y);
   if (diff!=0) return diff;
}
if (entry1.length==entry2.length) return 0;
else return entry1.length-entry2.length;

The code above also handles the case where the arrays might not have the same number of columns.

1

Here is an example:

public static class ColumnComparator<T extends Comparable>
        implements Comparator<T[]> {
    private int column;

    public ColumnComparator(int column) {
        this.column = column;
    }

    public int compare(T[] o1, T[] o2) {
        return o1[column].compareTo(o2[column]);
    }
}

Used like this:

Arrays.sort(myarray, new ColumnComparator<String>(1));

It can easily be generalized to compare more than one column.

0

I think the easiest way is to use a temporary array of strings since you have array of strings -

Sample array:

String array[][][] = {
        {
            {"Hello", "World"},
            {"Apple", "Orange"}
        },
        {
            {"Zebra", "Cow"},
            {"Cat", "Ball"}
        },
        {
            {"Elephant", "Whale"},
            {"Lion", "Monkey"}
        }                
};

Temporary array of strings:

String[] tempArray = new String[array.length * array[0].length * array[0][0].length];

Fill the temporary array:

int tempIndex = 0;
for(int i=0; i<array.length; i++) {
    for(int j=0; j<array[i].length; j++) {
        for(int k=0; k<array[i][j].length; k++) {
            System.out.println("array[" + i + "][" + j + "][" + k + "]: " + array[i][j][k]);
            tempArray[tempIndex++] = array[i][j][k];
        }
    }
}

That also prints -

array[0][0][0]: Hello
array[0][0][1]: World
array[0][1][0]: Apple
array[0][1][1]: Orange
array[1][0][0]: Zebra
array[1][0][1]: Cow
array[1][1][0]: Cat
array[1][1][1]: Ball
array[2][0][0]: Elephant
array[2][0][1]: Whale
array[2][1][0]: Lion
array[2][1][1]: Monkey

Now, sort the temp array -

Arrays.sort(tempArray);

Fill the original array with sorted values -

tempIndex = 0;
for(int i=0; i<array.length; i++) {
    for(int j=0; j<array[i].length; j++) {
        for(int k=0; k<array[i][j].length; k++) {                    
            array[i][j][k] = tempArray[tempIndex++];
        }
    }
}

Now the if you print the elements of the original array again, it should print -

array[0][0][0]: Apple
array[0][0][1]: Ball
array[0][1][0]: Cat
array[0][1][1]: Cow
array[1][0][0]: Elephant
array[1][0][1]: Hello
array[1][1][0]: Lion
array[1][1][1]: Monkey
array[2][0][0]: Orange
array[2][0][1]: Whale
array[2][1][0]: World
array[2][1][1]: Zebra

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.