Sorting a Two-Dimensional Array by Second Value

Sorting a two-dimensional array by second value

arr = [[:z,1], [:d,3], [:e,2]]
arr.sort {|a,b| a[1] <=> b[1]}
# => [[:z, 1], [:e, 2], [:d, 3]]

Or as user @Phrogz points out, if the inner arrays have exactly two elements each:

arr.sort_by{|x,y|y} # => [[:z, 1], [:e, 2], [:d, 3]]
arr.sort_by(&:last) # => [[:z, 1], [:e, 2], [:d, 3]]

Sort a 2D array by the second value

You can provide sort with a comparison function.

showIt.sort(function(a, b) {
return a[1] - b[1];
});

a and b are items from your array. sort expects a return value that is greater than zero, equal to zero, or less than zero. The first indicates a comes before b, zero means they are equal, and the last option means b first.

How to sort a two-dimensional array (2d array) by date in ruby on rails?

You can sort 2d arrays with sort_by method: sort_by sorts in ascending order by default. If you want it to be in descending order, just reverse the result.

array.sort_by{ |a| a.first }.reverse

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

Sort multidimensional array based on 2nd element of the subarray

list.sort, sorted accept optional key parameter. key function is used to generate comparison key.

>>> sorted(lst, key=lambda x: x[1], reverse=True)
[['I', 219], ['A', 22], ['P', 14], ['V', 13], ['G', 10], ...]

>>> sorted(lst, key=lambda x: -x[1])
[['I', 219], ['A', 22], ['P', 14], ['V', 13], ['G', 10], ...]

>>> import operator
>>> sorted(lst, key=operator.itemgetter(1), reverse=True)
[['I', 219], ['A', 22], ['P', 14], ['V', 13], ['G', 10], ...]

C++ Sort Array by 2nd Value

The short answer is: don't do that. C++ inherited it's built-in array from C, and it simply isn't really a very good fit for what you're trying to do.

Something that's reasonably similar and easy to implement would be to use std::vector instead of arrays.

std::vector<std::vector<int>> someVector {
{4, 20},
{1, 4},
{7, 15},
{8, 8},
{8, 1}
};

Sorting this based on the second item in each row is pretty trivial:

    std::sort(someVector.begin(), someVector.end(), 
[](auto const &a, auto const &b) { return a[1] < b[1]; });

We can then print out the result to verify that it's working as expected:

for (auto const &row : someVector)
std::cout << row[0] << "\t" << row[1] << "\n";

As you'd expect, this produces:

8   1
1 4
8 8
7 15
4 20

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

Need help sorting two dimensional arrays by second element and then by first element (Java)

Solution 1: sort the arrays by the second element, and then sort the arrays by the first element. Since Arrays.sort is stable, that's equivalent to first comparing by the first element, then the second.

Solution 2: modify your comparator as follows:

Arrays.sort(hand, new Comparator<int[]>() {
public int compare(int[] o1, int[] o2) {
if (o1[0] == o2[0]) {
return Integer.compare(o1[1], o2[1]);
} else {
return Integer.compare(o1[0], o2[0]);
}
}
});

or, with Guava (disclosure: I contribute to Guava), you can just write the comparator as

  public int compare(int[] o1, int[] o2) {
return ComparisonChain.start()
.compare(o1[0], o2[0])
.compare(o1[1], o2[1])
.result();
}


Related Topics



Leave a reply



Submit