oslon Posted October 23, 2024 Share Posted October 23, 2024 int[][] wh2d = { {0, 34}, {1, 28}, {2, 20}, {3, 31}, {4, 32}, {5, 28}, {6, 37}, {7, 41} }; I need to sort this based on 34.28,20...etc. Quote Link to comment https://forums.phpfreaks.com/topic/325152-sort-a-8x2-array-based-on-values-at-2nd-column-how/ Share on other sites More sharing options...
Barand Posted October 23, 2024 Share Posted October 23, 2024 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]; } Quote Link to comment https://forums.phpfreaks.com/topic/325152-sort-a-8x2-array-based-on-values-at-2nd-column-how/#findComment-1638612 Share on other sites More sharing options...
gizmola Posted February 24 Share Posted February 24 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} }; Quote Link to comment https://forums.phpfreaks.com/topic/325152-sort-a-8x2-array-based-on-values-at-2nd-column-how/#findComment-1650426 Share on other sites More sharing options...
gizmola Posted February 25 Share Posted February 25 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. Quote Link to comment https://forums.phpfreaks.com/topic/325152-sort-a-8x2-array-based-on-values-at-2nd-column-how/#findComment-1650440 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.