How to Sort 2 Dimensional Array by Column Value

How to sort multidimensional array by column?

Yes. The sorted built-in accepts a key argument:

sorted(li,key=lambda x: x[1])
Out[31]: [['Jason', 1], ['John', 2], ['Jim', 9]]

note that sorted returns a new list. If you want to sort in-place, use the .sort method of your list (which also, conveniently, accepts a key argument).

or alternatively,

from operator import itemgetter
sorted(li,key=itemgetter(1))
Out[33]: [['Jason', 1], ['John', 2], ['Jim', 9]]

Read more on the python wiki.

Sort a 2d array by the first column and then by the second one

Essentially what you want to do is to compare the inner arrays lexicographically (like how words are ordered in a dictionary). Arrays.compare does exactly that.

Arrays.sort(arrs, Arrays::compare);

java Arrays.sort 2d array

Use Overloaded Arrays#Sort(T[] a, Comparator c) which takes Comparator as the second argument.

double[][] array= {
{1, 5},
{13, 1.55},
{12, 100.6},
{12.1, .85} };

java.util.Arrays.sort(array, new java.util.Comparator<double[]>() {
public int compare(double[] a, double[] b) {
return Double.compare(a[0], b[0]);
}
});

JAVA-8: Instead of that big comparator, we can use lambda function as following-

Arrays.sort(array, Comparator.comparingDouble(o -> o[0]));

Sorting multi dimensional array by 2 columns - JavaScript

It is fairly straight forward:

If the strings are equal, then break the tie by comparing the integer values, else return the result of localeCompare

var arr = [
['ABC', 87, 'WHAT'],
['ABC', 34, 'ARE'],
['DEF', 13, 'YOU'],
['ABC', 18, 'DOING'],
['ABC', 34, 'DOING'],
['DEF', 24, 'TODAY'],
['ABA', 18, 'TODAY'],
['ABA', 11, 'TODAY']
];

function doSort(ascending) {
ascending = typeof ascending == 'undefined' || ascending == true;
return function(a, b) {
var ret = a[0].localeCompare(b[0]) || a[1] - b[1];
return ascending ? ret : -ret;
};
}

// sort ascending
arr.sort(doSort());
// sort descending
arr.sort(doSort(false));

Fiddle

How to sort a 2 dimensional array (jagged) by column

I found an answer posted where someone had asked a similar question but about rectangular arrays. This is a nice little method using Array.Sort with a lambda expression:

Array.Sort(contests, (a, b) => { return a[0] - b[0]; });

Which will sort a 2D array by the first column and keep the corresponding data in the second column (like sort rows by ID). If any row has the same value on the left side but different values on the right, then it will sort by value in the second column within those rows.

If you enter a 1 for the first value then it will sort by the right column:

Array.Sort(contests, (a, b) => { return a[1] - b[0]; });

Sorting a 2D Integer array based on a column

It works fine for me. To reverse order you'd negate the original compareTo, or swap the variables, but not both.

We'll probably need to see the rest of the code to understand why you're seeing what you're seeing; I cut and pasted your code verbatim, so odds are good the issue lies elsewhere.


dump(theArray);
Arrays.sort(theArray, new Comparator<Integer[]>() {
public int compare(Integer[] int1, Integer[] int2) {
Integer numOfKeys1 = int1[1];
Integer numOfKeys2 = int2[1];
return numOfKeys1.compareTo(numOfKeys2);
}
});
System.out.println("================");
dump(theArray);

0 10
0 10
1 9
2 9
3 9
4 15
5 10
6 4
7 8
8 11
9 12
================
6 4
7 8
1 9
2 9
3 9
0 10
0 10
5 10
8 11
9 12
4 15


Related Topics



Leave a reply



Submit