|
|
|
|
|
by tigershark
3105 days ago
|
|
No idea about C++ but in C# seems quite more readable (and flexible) someList.OrderByDescending(x => x.weight).ThenByDescending(x => x.Name);
It's quite similar to expressing the concept in English, certainly more than using list comprehension in Python.And how would you order it in Python by ascending on the first field and descending on the second using list comprehension? |
|
In answer to your question though,
appears to work. This does use a non-obvious trick, but being more explicit is a smidge difficult, since the key function is called only n times, as opposed to O(nlogn) in the C# example.Alternatively, you can use
Which is more like the original example, and if you're doing it in place, you get Python's builtin sort is timsort, so despite sorting the list twice, this will still run in approximately NlogN comparisons, not 2NlogN.You could also manually define a custom comparator, ie
and pass it to `functools.cmp_to_key`.