0

I have a multidimensional array with double values that I would like to sort..

//declare array    
standingsB = new Double[10][2];

//populate array from the temparray created during read from file
arryLgt = 0;
        for (int row = 0; row < standingsB.length; row++){

            for (int column = 0; column < standingsB[row].length; column++) {


                standingsB[row][column] = Double.parseDouble(tempStandingsArray[arryLgt]);
                arryLgt = arryLgt + 1;
            }
        }

The array has values such as [1.5,7.0] [4.2,4.0] etc...

For the next part I don't really know how it works but from reading other articles here this is the best as I can copy without knowledge

Arrays.sort(standingsB, new Comparator<Double[]>() {
            @Override
            public int compare(Double[] s1, Double[] s2) {
                compare(s1, s2);
            }
        });

The above fails to compile (with missing return statement) which is to be expected as I have no idea on how to use the Arrays.sort with a comparator. But I'm not even sure if I'm on the right page being as new to Java (and programing in general) as I am.

Thanks for looking!

3
  • You need to write return compare(s1, s2);. However, you need to tell us what it means for one Double[] to be 'greater than' or 'less than' another one. Commented Jul 8, 2013 at 22:14
  • Or give us an example input/output (along with some explanation). Commented Jul 8, 2013 at 22:16
  • What do you mean by sorting a two dimensional array? {{3,5,2},{1,4,6}} --> {{1,2,3},{4,5,6}}? Commented Jul 8, 2013 at 23:49

4 Answers 4

4

You're pretty close. Your comparator will depend on what order you want your results in. Let's say you want the rows to be sorted in the natural order of the first element in each row. Then your code would look like:

Arrays.sort(standingsB, new Comparator<Double[]>() {
    public int compare(Double[] s1, Double[] s2) {
        if (s1[0] > s2[0])
            return 1;    // tells Arrays.sort() that s1 comes after s2
        else if (s1[0] < s2[0])
            return -1;   // tells Arrays.sort() that s1 comes before s2
        else {
            /*
             * s1 and s2 are equal.  Arrays.sort() is stable,
             * so these two rows will appear in their original order.
             * You could take it a step further in this block by comparing
             * s1[1] and s2[1] in the same manner, but it depends on how
             * you want to sort in that situation.
             */
            return 0;
        }
    }
};
Sign up to request clarification or add additional context in comments.

1 Comment

Thank You! The code works now as I want it to! I'm going to put in some println's in there so that I can understand what is being passed and what is being compared in the s1[0] and s2[0] at this point they are just some magic/voodoo making things sort! :)
1

I think the answer provided by @Tap doesn't fulfill the askers question to 100%. As described, the array is sorted for its value at the first index only. The result of sorting {{2,0},{1,2},{1,1}} would be {{1,2},{1,1},{2,0}} not {{1,1},{1,2},{2,0}}, as expected. I've implemented a generic ArrayComparator for all types implementing the Comparable interface and released it on my blog:

public class ArrayComparator<T extends Comparable<T>> implements Comparator<T[]> {
    @Override public int compare(T[] arrayA, T[] arrayB) {
        if(arrayA==arrayB) return 0; int compare;
        for(int index=0;index<arrayA.length;index++)
            if(index<arrayB.length) {
                if((compare=arrayA[index].compareTo(arrayB[index]))!=0)
                    return compare;
            } else return 1; //first array is longer
        if(arrayA.length==arrayB.length)
             return 0; //arrays are equal
        else return -1; //first array is shorter 
    }
}

With this ArrayComparator you can sort multi-dimensional arrays:

String[][] sorted = new String[][]{{"A","B"},{"B","C"},{"A","C"}};
Arrays.sort(sorted, new ArrayComparator<>());

Lists of arrays:

List<String[]> sorted = new ArrayList<>();
sorted.add(new String[]{"A","B"});
sorted.add(new String[]{"B","C"});
sorted.add(new String[]{"A","C"});
sorted.sort(new ArrayComparator<>());

And build up (Sorted)Maps easily:

Map<String[],Object> sorted = new TreeMap<>(new ArrayComparator<>());
sorted.put(new String[]{"A","B"}, new Object());
sorted.put(new String[]{"B","C"}, new Object());
sorted.put(new String[]{"A","C"}, new Object());

Just remember, the generic type must implement the Comparable interface.

Comments

0

Solution with lambda sorting array of int[][] contests example :

Arrays.sort(contests, (a, b)->Integer.compare(b[0], a[0]));

Comments

-2

Arrays.sort() expects a single dimensional array while in your case you are trying to pass a multidimensional array.

eg Double[] d = {1.0,5.2,3.2};

Then you use Arrays.sort(d) since the sort can work on the primitive types or the wrapper types.

1 Comment

Multidimensional arrays are single-dimensional arrays, i.e. arrays of arrays. So you can pass them to Arrays.sort(). The element types will be themselves arrays, and you have to define a Comparator that works on those arrays.

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.