Sort a Two Dimensional Array Based on One Column

Sort a two dimensional array based on one column

Sort a two dimensional array based on one column

The first column is a date of format "yyyy.MM.dd HH:mm" and the second column is a String.

Since you say 2-D array, I assume "date of format ..." means a String. Here's code for sorting a 2-D array of String[][]:

import java.util.Arrays;
import java.util.Comparator;

public class Asdf {

public static void main(final String[] args) {
final String[][] data = new String[][] {
new String[] { "2009.07.25 20:24", "Message A" },
new String[] { "2009.07.25 20:17", "Message G" },
new String[] { "2009.07.25 20:25", "Message B" },
new String[] { "2009.07.25 20:30", "Message D" },
new String[] { "2009.07.25 20:01", "Message F" },
new String[] { "2009.07.25 21:08", "Message E" },
new String[] { "2009.07.25 19:54", "Message R" } };

Arrays.sort(data, new Comparator<String[]>() {
@Override
public int compare(final String[] entry1, final String[] entry2) {
final String time1 = entry1[0];
final String time2 = entry2[0];
return time1.compareTo(time2);
}
});

for (final String[] s : data) {
System.out.println(s[0] + " " + s[1]);
}
}

}

Output:

2009.07.25 19:54 Message R
2009.07.25 20:01 Message F
2009.07.25 20:17 Message G
2009.07.25 20:24 Message A
2009.07.25 20:25 Message B
2009.07.25 20:30 Message D
2009.07.25 21:08 Message E

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

how to sort a multidimensional array with one column in ascending order and another column in descending order?

You can use the key but with a small change, and using - counts as "descending" (negative numbers always come before positive numbers in an ascending order sort). So you don't have to use the reverse parameter at all.

>>> sorted(students, key = lambda x:(-x[2], x[1]))
[[2, 'Rose', 13], [3, 'Jack', 12], [5, 'Sam', 12], [1, 'Tom', 10], [4, 'Joy', 8]]

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

Sorting two dimensional array by more than one condition for each column

You're on the right lines with your Comparator<String[]>.

You can compose Comparators with .thenComparing(), so you could do:

Comparator<String[]> byName = Comparator.comparing( row -> row[1] );
Comparator<String[]> byGPA = Comparator.comparing( row -> Double.parseDouble(row[2]) );

Arrays.sort(a, byName.thenComparing(byGPA));

Actually you can do this more compactly with:

Arrays.sort(a, Comparator.comparing( row -> row[1] )
.thenComparing( row -> Double.parseDouble(row[2])));

Working with user input, composition like this is ideal - compose at runtime based on what the user inputs.

Sorting two dimensional array in C based only on the first column

Try declaring the function as
sort_double_array(double A[][2], int n);

Multidimensional array must have bounds for all dimensions except the first.



Related Topics



Leave a reply



Submit