Closed Nyapaw closed 11 months ago
For example, you might want to sort the price of the item in ascending order, and then the name of the item in lexicographic order.
It’ll be helpful to provide a different use case where sorting with multiple passes is truly needed, because the example can be done by defining both criteria in the custom comparator.
I've implemented a non-native stable sorting algorithm in Luau based on a merge sort algorithm very similar to C++'s std::stable_sort implementation.
I wrote:
Previously, I've made a feature request on the relative worst-case inefficiency of
table.sort
compared to other compiler implementations in other languages, and I am glad that got addressed recently; table.sort now uses introspection to guarantee a O(n*log(n)) worst-case time complexity.However, introspective sort, being a derivative of quicksort and heapsort, does not provide stability. I'd like to propose a new method for the
table
library, preferablytable.stablesort(tbl, comparer)
.As for the choice of algorithm, in many languages (such as Java, Python, and JavaScript) the compiler or interpreter implements Timsort, an adaptive version of merge sort. It guarantees O(n*log(n)) worst-case time complexity.