Sort Multidimensional Array Based on 2Nd Element of the Subarray

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

Sort 2D array by matching the last values with the first one in the next index

You could take an object as reference and rebuild the array by taking the chained items.

const
getItems = (reference, value) => {
const a = reference[value];
return a ? [a, ...(a[1] === 0 ? [] : getItems(reference, a[1]))] : [];
},
array = [[1, 0], [2, 1], [0, 3], [3, 2]],
reference = array.reduce((r, a) => (r[a[0]] = a, r), {}),
result = getItems(reference, 0);

console.log(result);

Python Sort Multidimensional List first element Based on second element that already sorted

Let me get this straight, you want a list to be sorted first based on the second element in a tuple in ascending order, and break any ties by the first element in descending order?

You can do that with sorted(l, key=lambda x: (x[1], -x[0])), assuming your elements are numbers. If not you have to write a more complex comparison function.

JS: Sort a multi-dimensional array based on each sub-array's length

You can just use sort on multi directly:

multi.sort(function (a, b) {
return b.length - a.length;
});

How to Sort a Multi-dimensional Array by Value

Try a usort. If you are still on PHP 5.2 or earlier, you'll have to define a sorting function first:

function sortByOrder($a, $b) {
return $a['order'] - $b['order'];
}

usort($myArray, 'sortByOrder');

Starting in PHP 5.3, you can use an anonymous function:

usort($myArray, function($a, $b) {
return $a['order'] - $b['order'];
});

With PHP 7 you can use the spaceship operator:

usort($myArray, function($a, $b) {
return $a['order'] <=> $b['order'];
});

Finally, in PHP 7.4 you can clean up a bit with an arrow function:

usort($myArray, fn($a, $b) => $a['order'] <=> $b['order']);

To extend this to multi-dimensional sorting, reference the second/third sorting elements if the first is zero - best explained below. You can also use this for sorting on sub-elements.

usort($myArray, function($a, $b) {
$retval = $a['order'] <=> $b['order'];
if ($retval == 0) {
$retval = $a['suborder'] <=> $b['suborder'];
if ($retval == 0) {
$retval = $a['details']['subsuborder'] <=> $b['details']['subsuborder'];
}
}
return $retval;
});

If you need to retain key associations, use uasort() - see comparison of array sorting functions in the manual.



Related Topics



Leave a reply



Submit