mardi 5 mai 2015

Sorting Array by biggest number

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  
4  148  0  
3  56  100  
2  102  100  
6  65  200  
5  125  200 

I want my array to sort B by the biggest number depending A like this example.

P  B    A 
4  148  0    
1  135  0  
2  102  100 
3  56  100  
5  125  200    
6  65  200

This is currently my code

 Arrays.sort(x, 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.compare(o1[1], o2[1]);
                    }
                    return (ret);
                }
            });

Aucun commentaire:

Enregistrer un commentaire