Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Procedure to sort a two dimensional int array depending on column

The array I have before and how we want it after the sorting:

Before:

Box    Weight    Priority
1       50          5
2       30          8
3       90          6
4       20          7  
5       80          9

After:

Box    Weight    Priority
3       90          6
5       80          9
1       50          5
2       30          8
4       20          7

We work in the int matrix:

data= new int[BoxNumber][3];

The sorting is based in the second column Weight. I am looking for a procedure that sorts the data array.

 public void sortC(int[][] temp)
{
    if (temp.length >= 2)
    {
        for (int i = 1; i <= temp.length - 1; i++)
        {
            int[] hold = temp[i];
            int[] holdP = temp[i-1];

            int j = i;

            while (j > 0 && hold[1] < holdP[1]) // 1 represents the reference of sorting
            {
                hold = temp[j];
                holdP = temp[j-1];

                temp[j] = holdP;
                temp[j-1] = hold;

                j--;
            }
        }
    }
}

 sortC(data);

I tried this one, but unfortunately it doesn't give a right sorting I couldn't figure out the pickle.

like image 303
Ghassen Bellagha Avatar asked Aug 14 '13 13:08

Ghassen Bellagha


People also ask

How do you sort a two-dimensional array by column value?

To sort 2 dimensional array by column value with JavaScript, we can use the array sort method. const arr = [ [12, "AAA"], [12, "BBB"], [12, "CCC"], [28, "DDD"], [18, "CCC"], [12, "DDD"], [18, "CCC"], [28, "DDD"], [28, "DDD"], [58, "BBB"], [68, "BBB"], [78, "BBB"], ]; const sortedArr = arr.

How do you sort numbers in 2D arrays?

Make the 2D array into a separate simple (1D) array (STEP 1). Then use the Arrays. sort() method to sort the simple array (STEP 2). Then set each space of the 2D array to be the number of columns across (X-coordinate where the space will be changed) multiplied by the number of spaces per row in the 2D array.

How do you sort a 2D array using sort function?

Ways to Sort a 2D Vector In sort(), it generally takes two parameters, the first one being the point of the array/vector from where the sorting needs to begin and the second parameter being the length up to which we want the array/vector to get sorted. This function is included in <algorithm> header file.

How does arrays sort work on 2D array?

Example for 2D array sorting in Java to sort all elements of a 2D Array. As in the above program, the sort() method is used to iterate each element of a 2D array, and when the current element is greater than the next element, then swap the numbers. Finally, the print method displays all the elements of the 2D array.


3 Answers

Use java.util.Arrays.sort with a custom Comparator.

int[][] temp = { { 1, 50, 5 }, { 2, 30, 8 }, { 3, 90, 6 },
        { 4, 20, 7 }, { 5, 80, 9 }, };
Arrays.sort(temp, new Comparator<int[]>() {
    @Override
    public int compare(int[] o1, int[] o2) {
        return Integer.compare(o2[1], o1[1]);
    }
});

As shmosel mentioned below, with Java 8, you can use:

Arrays.sort(temp, Comparator.comparingInt(arr -> arr[1]));
like image 126
johnchen902 Avatar answered Oct 13 '22 01:10

johnchen902


You can do this instead of writing your own sorting algorithm:

int[][] n = new int[10][];
//init your array here

List<int[]> ints = Arrays.asList(n);
Collections.sort(ints, new Comparator<int[]>() {
    @Override
    public int compare(int[] o1, int[] o2) {
        return o1[1] - o2[1]; // compare via second column
    }
});

and if you want make it array again:

int[][] result = ints.toArray(n);
like image 38
Tala Avatar answered Oct 12 '22 23:10

Tala


Arrays.sort(boxTypes, (a, b) -> b[1] - a[1]);

or Using a priority queue

PriorityQueue<int[]> queue = new PriorityQueue<>((a, b)->b[1] - a[1]);

like image 29
Purva Avatar answered Oct 13 '22 01:10

Purva