0

This is my unsorted array:

P  B    A 
5  125  400
2  102  145
3  56   200
6  65   200
7  30   200
4  148  300
1  135  0

This is what my current array has after I sorted it this is the output I get

P  B    A 
1  135  0 
2  102  145 
3  56   200 
6  65   200 
7  30   200 
4  148  300 
5  125  400

And I would like the output to look like this

P  B    A 
1  135  0 
2  102  145
7  30   200 
3  56   200 
6  65   200 
4  148  300 
5  125  400

This is currently my code

Arrays.sort(myArr, new Comparator<int[]>() {
    public int compare(int[] o1, int[] o2) {
        return Integer.compare(o1[2], o2[2]);
    }
});
6
  • And the problem with your code/your question is... Commented Apr 13, 2015 at 16:39
  • How can an array have characters (PBA) and ints? How are the ints stored in the array? Commented Apr 13, 2015 at 16:41
  • Do you want it sorted by A then B? Commented Apr 13, 2015 at 16:44
  • it doesn't thats the output Commented Apr 13, 2015 at 16:44
  • yes thats what i want Commented Apr 13, 2015 at 16:45

3 Answers 3

2

You need to further specify the Comparator. At the moment, you are only sorting the array by the 3rd element.

Arrays.sort(myArr, new Comparator<int[]>() {
    public int compare(int[] o1, int[] o2) {
        int ret = Integer.compare(o1[2], o2[2]);
        // if the entries are equal at index 2, compare index 1
        if (0 == ret) {
            ret = Integer.compareTo(o1[1], o2[1]);
        }
        return (ret);
    }
});
Sign up to request clarification or add additional context in comments.

Comments

1

First, you are storing your int arrays in another array, presumably in a int[][]. You are attempting to enforce a relationship between the 3 elements of your inner arrays. In an object-oriented language like Java, it is better to define a class to store the 3 values together, and then to create a one-dimensional array to store the objects.

public class My3Values {
    private int p, b, a;
    // Constructor(s), getters go here
}

(It doesn't have to be called My3Values, it's just for example.)

Then you can define your Comparator class as a Comparator<My3Values>.

Second, you are currently ignoring the B value that means something for your sort. Add code that considers the B value if the A values are identical.

int comp = Integer.compare(o1.getA(), o2.getA());
if (comp == 0)
{
    comp = Integer.compare(o1.getB(), o2.getB());
}
return comp;

Comments

1

You need to handle case where comparing second row will return 0 (equal values) and in that case return result of comparing values from first columns.

So your code can be chanced do something like:

Arrays.sort(myArr, new Comparator<int[]>() {
    public int compare(int[] o1, int[] o2) {
        int result = Integer.compare(o1[2], o2[2]);
        if (result != 0) 
            return result;
        else 
            return Integer.compare(o1[1], o2[1]);
    }
});

But if it is possible that you will want to compare more than one column and if you want to easily create different orders you should create separate comparator for each column.

In Java 8 you can rewrite comparator like

Comparator<int[]> secondColumnt = new Comparator<int[]>() {
    public int compare(int[] o1, int[] o2) {
        return Integer.compare(o1[2], o2[2]);
    }
}

by writing it as

Comparator<int[]> secondColumnt = Comparator.comparingInt(row -> row[2]);

so lets also create comparator for first column

Comparator<int[]> firstColumnt = Comparator.comparingInt(row -> row[1]);

Now since Java 8 we can create comparators from already existing comparators using thenComparing method.

So if we want to create comparator which will first compare values in second columns and if they are equal comparator for first first column we can write it as

Comparator<int[]> secondThenFirstColumn = secondColumnt.thenComparing(firstColumnt);    

So your final code can look like

Arrays.sort(myArr, secondThenFirstColumn);

or

Arrays.sort(myArr, secondColumnt.thenComparing(firstColumnt));

or if you don't want to create separate predefined comparators

Arrays.sort(myArr, Comparator.<int[]>comparingInt(row -> row[2]).thenComparingInt(row -> row[2]));

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.