Jump to content

Recommended Posts

You need to use a custom sort function with usort()

usort($array['wh2d'], fn($a,$b)=>$a[1]<=>$b[1]);

or, if you are uncomfortable with that syntax, ...

usort($array'wh2d', 'mysort');

function mysort($a, $b)
{
    return $a[1] <=> $b[1];
}


 

  • 4 months later...

Barry, the op posted some java code unfortunately. 

OP Got the right sub forum, but neglected to include that information.

 

int[][] wh2d = {
  {0, 34},
  {1, 28},
  {2, 20},
  {3, 31},
  {4, 32},
  {5, 28},
  {6, 37},
  {7, 41}
};

 

It's been a long time since I've written any Java code, so it took me a while just to get things setup to investigate this, but here you go:

 

import java.util.Arrays;

public class Main {
  public static void main(String args[]) {
    int[][] wh2d = {
      {0, 34},
      {1, 28},
      {2, 20},
      {3, 31},
      {4, 32},
      {5, 28},
      {6, 37},
      {7, 41}
    };
    
    System.out.println("Before sort----");
    for (int i = 0; i < wh2d.length; i++) {
        System.out.println(Arrays.toString(wh2d[i]));
    }
    System.out.println("-------\n\n");
    
    Arrays.sort(wh2d, (a, b) -> {
        System.out.println("----Lambda------");
        System.out.println("a[0]:" + a[0]);
        System.out.println("a[1]:" + a[1]);
        
        System.out.println("b[0]:" + b[0]);
        System.out.println("b[1]:" + b[1]);
        
        if (b[1] == a[1]) {
            System.out.println("eq-lval:" + (b[0] - a[0]));
            return a[0] - b[0];
        }
        System.out.println("ne-lval:" + (b[1] - a[1]));
        return a[1] - b[1];
    });
    
    System.out.println("\n\nAfter sort----");
    for (int i = 0; i < wh2d.length; i++) {
        System.out.println(Arrays.toString(wh2d[i]));
    }
    
  }
}

Results:

 Command Line Arguments
   
Before sort----
[0, 34]
[1, 28]
[2, 20]
[3, 31]
[4, 32]
[5, 28]
[6, 37]
[7, 41]
-------


----Lambda------
a[0]:1
a[1]:28
b[0]:0
b[1]:34
ne-lval:6
----Lambda------
a[0]:2
a[1]:20
b[0]:1
b[1]:28
ne-lval:8
----Lambda------
a[0]:3
a[1]:31
b[0]:2
b[1]:20
ne-lval:-11
----Lambda------
a[0]:3
a[1]:31
b[0]:1
b[1]:28
ne-lval:-3
----Lambda------
a[0]:3
a[1]:31
b[0]:0
b[1]:34
ne-lval:3
----Lambda------
a[0]:4
a[1]:32
b[0]:3
b[1]:31
ne-lval:-1
----Lambda------
a[0]:4
a[1]:32
b[0]:0
b[1]:34
ne-lval:2
----Lambda------
a[0]:5
a[1]:28
b[0]:3
b[1]:31
ne-lval:3
----Lambda------
a[0]:5
a[1]:28
b[0]:1
b[1]:28
eq-lval:-4
----Lambda------
a[0]:6
a[1]:37
b[0]:3
b[1]:31
ne-lval:-6
----Lambda------
a[0]:6
a[1]:37
b[0]:0
b[1]:34
ne-lval:-3
----Lambda------
a[0]:7
a[1]:41
b[0]:3
b[1]:31
ne-lval:-10
----Lambda------
a[0]:7
a[1]:41
b[0]:0
b[1]:34
ne-lval:-7
----Lambda------
a[0]:7
a[1]:41
b[0]:6
b[1]:37
ne-lval:-4


After sort----
[2, 20]
[1, 28]
[5, 28]
[3, 31]
[4, 32]
[0, 34]
[6, 37]
[7, 41]

 

I suspect that the confusion in this code might come from the declaration of:

int[][] wh2d = {
  {0, 34}
};

//vs
int[][] wh2d = {
  {0, 34, 90, 75}
};

These are both valid definitions, because the 2nd dimension is an array of integers. 

I suspect the confusion might come from thinking that the first dimension of the array will be the element inside the pairs of numbers provided for the 2nd array.  That would be an incorrect assumption.  The first array dimension is simply numerically indexed from 0..n items.

Given that the 2nd dimension contains arrays with 2 items, and in each case you want to compare the 2nd element, you need to use a custom "Comparator".  

I'm reminded at hard it is to do simple things in Java and C++, when compared to interpreted languages like Python, Javascript and PHP.  

The important thing to understand is that the Lambda is a stand in for an integer Comparator, which requires the return of an integer value (+, 0, -).  Based on that return, the elements in the first dimension will be swapped (or not).   I put in a lot of debugging output in the Lambda, so you can hopefully follow what is happening in the lambda.

 

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.