1

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]
3
  • 2
    You want to sort your array of array just based on the first array? can you please show us an input output example? Commented Feb 22, 2019 at 18:55
  • Okay, it look 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] Commented Feb 22, 2019 at 20:53
  • You can edit your question to add that infos Commented Feb 22, 2019 at 20:58

4 Answers 4

2

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]
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your help, but the sorting changes the array from: [0, 0] [4, 3] [3, 14] [0, 0] [0, 0] [0, 0] [0, 0] [0, 0] [0, 0] [0, 0] [0, 0] [0, 0] [0, 0] [0, 0] [0, 0] [0, 0] [0, 0] to[0, 2] [1, 4] [1, 1] [1, 0] [2, 4] [2, 4] [3, 1] [4, 4] [5, 1] [5, 9] [6, 1] [6, 6] [7, 5] [7, 1] [8, 7] [9, 6] [9, 6] in the original array are many empty fields, but after sorting all of them are filled.
@GrobMo you want to ignore the fields which contains [0, 0]?
1

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]

Comments

0

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

Comments

0

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);

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.