Ben Chuanlong Du's Blog

It is never too late to learn.

Sort Collections in Python

Tips

  1. You can use the built-in function sorted to sort any iterable collection. It always a (new) list containing sorted data. Some mutable collections (e.g., list) have thee method sort to sort elements in-place. Both sorted and Collection.sort accept an argument key for specifying customized sorting criteria.

Sort a List

Sort a List in Kotlin

sorted

If elements in a List implements the Comparable interface, you can call the method sorted to return a sorted List (in nature order).

Note: Pair in Kotin does not implement the Comparable interface!!!

Sorting Functions in C++

Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!

std::sort std::stable_sort std::partial_sort

Notice that only std::stable_sort is stable sort (at the cost of additional time/space complexity). It is easy to achive stable sort using std …