-1

I have one 2 dimensional array It's an array with 7 int in line and more lines.

int[][] new arr=new[7][100];

number in line is ranked.

I need a ranked array.

for example

9 4 15 22 32 47 50
1 5 9 12 19 25 36
22 23 25 29 36 55 99
1 5 11 12 19 25 36

after sort

1 5 9 12 19 25 36
1 5 11 12 19 25 36
9 4 15 22 32 47 50
22 23 25 29 36 55 99
Dharman
  • 30,962
  • 25
  • 85
  • 135
user7495572
  • 1
  • 1
  • 3
  • 1
    You may want to transpose your array first, so that each element is one line (i.e. int[100][7] instead of int[7][100]). Here is an example http://introcs.cs.princeton.edu/java/14array/Transpose.java.html – Tesseract Feb 11 '17 at 22:03

3 Answers3

1

The simple approach to solved this problem is transform your 2D array into List of 1D array.

List<int[]> list = new ArrayList<int[]>();
// add logic to transform your 2D array here

Then you can use Collections.sort() with custom Comparator function.

Collections.sort(list, new Comparator<int[]>() {
    public int compare(int []a,int []b) {
        for(int i=0;i<6;i++)
            if(a[i]!=b[i]) return a[i]-b[i];
        return a[6] - b[6];
    }
});
algojava
  • 743
  • 4
  • 9
0

I would do something like this,or something similar:

I saw something similar in Stack Overflow: https://stackoverflow.com/a/15452462/8024829.

 double[][] array= {
   {1, 5},
   {13, 1.55},
   {12, 100.6},
   {12.1, .85} };

   java.util.Arrays.sort(array, new java.util.Comparator<double[]>() {
       public int compare(double[] a, double[] b) {
           return Double.compare(a[0], b[0]);
       }
   });
Oron Zimmer
  • 336
  • 1
  • 3
0

You can try this simple expression:

Arrays.sort(A, (a, b) -> a[0] - b[0]);
Joe
  • 326
  • 3
  • 11