Recently while working for my project, I came across this situation when I had a 2-D array and I needed to sort it twice on 2 of its columns. Consider the following 2D array:
[cc lang=“java”] String[][] testString = new String[][] { {“1”, “2”, “6”}, {“4”, “5”, “3”} }; [/cc]
Sorting the above 2D array on zero column will give [cc lang=“java”] { {“1”, “2”, “6”}, {“4”, “5”, “3”} } [/cc]
whereas sorting it on second column will give [cc lang=“java”] { {“4”, “5”, “3”}, {“1”, “2”, “6”} } [/cc]
I did not want to do the most common as well as tedious thing, i.e. write my own sorting function like this: [cc lang=“java”] public void myOwnSort(String [][] exampleArray) { //code for sorting the array according to my wish. } [/cc]
I wanted to avoid this for 2 resons:
- I would have to write the code for it ( lazy me!!).
- Secondly, I can possibly never match the efficiency provided by the sorting functions of java.util.Arrays
So, my main aim was to look for a solution that would help me sort my 2-D array using the internal JAVA sorters.
I started digging into the JAVA docs (I must admit, I was a naive in JAVA at this point), and came across the Comparator Interface, using which we can tell the JAVA sorting function ( [Arrays.sort](http://java.sun.com/j2se/1.4.2/docs/api/java/util/Arrays.html#sort(java.lang.Object[], java.util.Comparator))) on how to compare two elements of a 2D array. One must understand that a 2D array is essentially a 1D array with its each element as another 1D array.
[cc lang=“java”] String[][] testString = new String[][] { {“1”, “2”, “6”}, {“4”, “5”, “3”} }; [/cc]
Above, there are two arrays containing 3 strings each. We must now compare these two arrays to determine their order in the resultant 2D array. We can compare these two 1-D arrays in any way we like, we can compare them on the first column, third column or on the average length of the strings (just anything!!). Let us implement Comparator interface to define such a compare function:
[cc lang=“java”] class 2DcolumnComparator implements Comparator { private int columnToSortOn;
//contructor to set the column to sort on. 2DcolumnComparator(int columnToSortOn) { this.columnToSortOn = columnToSortOn; }
// Implement the abstract method which tells // how to order the two elements in the array. public int compare(Object o1, Object o2) { // cast the object args back to string arrays String[] row1 = (String[])o1; String[] row2 = (String[])o2;
// compare the desired column values return row1[columnToSortOn].compareTo(row2[columnToSortOn]); } } [/cc]
Above, we create a class 2DcolumnComparator which implements comparator interface and compare 1D array elements on the column passed to the constructor. Now, we can pass an instance of this class to the Arrays.sort function, and the 2D array will sorted on the desired column.
[cc lang=“java”] public class Analysis {
public static void main(String args[]) { String[][] childrenArray = new String[MAXLIMIT][10];
//Populate the Array and sort Arrays.sort(childrenArray,new 2DcolumnComparator(5)); } } [/cc]
Here, we instantiate the 2DcolumnComaparator class and pass the column to sort on as parameter to its constructor. Above, the 2D array will be sorted on its fifth column (starting from zero). You can, of course, change the compare function to order elements according to your wish.
That is all I needed to sort a 2-D Array. Happy Sorting!!! :-)
Useful Resources: http://java.sun.com/j2se/1.4.2/docs/api/java/util/Comparator.html [http://java.sun.com/j2se/1.4.2/docs/api/java/util/Arrays.html#sort(java.lang.Object[], java.util.Comparator)](http://java.sun.com/j2se/1.4.2/docs/api/java/util/Arrays.html#sort(java.lang.Object[], java.util.Comparator))
Recommended Reading: Core Java(TM), Volume I–Fundamentals (8th Edition) Core Java(TM), Volume 2–Fundamentals (8th Edition)
Updates:
- If you need to sort a 2D array of objects of any class which implements Comparable, you can look at the comment by Lars.
- If you need to sort a multi-dimensional array on multiple columns, you can look at the comment by Mark.
Article last updated on 25 Feb 2010.