9

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.

0

3 Answers 3

10

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

4 Comments

the method u gave Arrays.sort works perfectly. Could you please mention some comments to explain the method for me because am not good into Comparator and Arrays.sort tools ? And Thank Your for your help
@GhassenBellagha Java docs: Arrays.sort
In Java 8, Arrays.sort(temp, Comparator.comparingInt(arr -> arr[1]));
@shmosel Thanks. Never revisited this post.
2

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

1 Comment

is there any other way where we could correct the procedure i mentioned ? Cause am not good into lists and collections .. ?
1

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

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.