简体   繁体   中英

How to sort a specific 2 dimensional Array in java

I'm trying to sort the following Array:

int hitlist[][] = new int [17][2];

Sorting information is always in hitlist[i][0] and it's numeric, but I can't find the correct way for Arrays.sort .

The input looks like:

[0, 0] 
[4, 0] 
[3, 1] 
[4, 2] 
[4, 4] 
[5, 6] 
[4, 7] 
[4, 8] 
[1, 9] 
[4, 11] 
[4, 12] 
[2, 13] 
[4, 14] 
[4, 15] 
[0, 0] 
[0, 0] 
[0, 0] 

and now I want it to be sorted like:

[1, 9]
[2, 13]
[3, 1]
[4, 0]
[4, 2] 
[4, 4]
[4, 7] 
[4, 8]
[4, 11] 
[4, 12]
[4, 14] 
[4, 15]

If you want to sort an array based on the index you can use Arrays::sort with Comparator::comparingInt

int index = 0;
Arrays.sort(hitlist, Comparator.comparingInt(arr -> arr[index]));

Here is an example in Ideone


Edit

Based on your comment and comment , you want to ignore the [0, 0] from your array after sorting, in this case you can use :

int[][] hitlist = {{0, 0}, {4, 0}, {3, 1}, {4, 2}, {4, 4}, {5, 6}, {4, 7}, {4, 8}, {1, 9}, {4, 11}, {4, 12}, {2, 13}, {4, 14}, {4, 15}, {0, 0}, {0, 0}, {0, 0}};
int index = 0;
int[][] sortedArray = Arrays.stream(hitlist)
        .filter(arr -> arr[0] != 0 && arr[1] != 0)
        .sorted(Comparator.comparingInt(arr -> arr[index]))
        .toArray(int[][]::new);

Ideone demo

Outputs

[1, 9]
[2, 13]
[3, 1]
[4, 2]
[4, 4]
[4, 7]
[4, 8]
[4, 11]
[4, 12]
[4, 14]
[4, 15]
[5, 6]

Although I prefer YCF_L's solution, this implementation utilizes a Quick-Sort with an integer array comparator. This offers more flexibility.

import java.util.Arrays;

/**
 * Based on Quicksort (right-most pivot) implementation from:  
 * https://www.programcreek.com/2012/11/quicksort-array-in-java/
 */
public class Sorter {
    private static interface IntArrayComparator {
        int compare(int[] a, int[] b);
    }

    public static void main(String[] args) {
        int hitlist[][] = new int[8][2];
        hitlist[4] = new int[] { 4, 10000 };
        hitlist[1] = new int[] { 1, 10 };
        hitlist[5] = new int[] { 5, 100000 };
        hitlist[0] = new int[] { 0, 1 };
        hitlist[2] = new int[] { 2, 100 };
        hitlist[7] = new int[] { 7, 10000000 };
        hitlist[3] = new int[] { 3, 1000 };
        hitlist[6] = new int[] { 6, 1000000 };

        quickSort(hitlist, (a, b) -> a[0] - b[0]);
        Arrays.asList(hitlist).stream().map(Arrays::toString).forEach(System.out::println);
    }

    public static void quickSort(int[][] arr, IntArrayComparator comparator) {
        quickSort(arr, comparator, 0, arr.length - 1);
    }

    public static void quickSort(int[][] arr, IntArrayComparator comparator, int start, int end) {
        int partition = partition(arr, comparator, start, end);
        if (partition - 1 > start) {
            quickSort(arr, comparator, start, partition - 1);
        }
        if (partition + 1 < end) {
            quickSort(arr, comparator, partition + 1, end);
        }
    }

    public static int partition(int[][] arr, IntArrayComparator comparator, int start, int end) {
        int[] pivot = arr[end];
        for (int i = start; i < end; i++) {
            if (comparator.compare(arr[i], pivot) < 0) {
                int[] temp = arr[start];
                arr[start] = arr[i];
                arr[i] = temp;
                start++;
            }
        }
        int[] temp = arr[start];
        arr[start] = pivot;
        arr[end] = temp;
        return start;
    }
}

Result

[0, 1]
[1, 10]
[2, 100]
[3, 1000]
[4, 10000]
[5, 100000]
[6, 1000000]
[7, 10000000]

It depends on whether you want to sort row or columns.

Lets say you want to sort each row, you can do.

for(int i=0; i < hitlist.size(); i++ {
     Array.sort(hitlist[i]);
}

sorting column becomes tricky, in which case you can construct an new array containing column values and sort or rotate the column to rows (90 degree), sort it as rows and rotate back again (-90degrees)

if you need anything else, you have to implement the search by yourself.

Hope this helps

You can box the int arrays into Integer arrays and then compare two Integer objects (the ones at 0th index) numerically in a lambda function.

And then simply pass that comparator to Arrays.sort which will sort it according to the order induced by the comparator.

    Integer[][] array= {
            {1, 3},
            {10, 5},
            {4, 100},
            {12, 30} };

    Comparator<Integer[]> arrayComparator = (a1, a2) -> a1[0].compareTo(a2[0]);

    Arrays.sort(array, arrayComparator);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM