38 Sorting
HardThere are two types of sorting in cangjie, Stable sort and Unstable sort. Stable sort ensures that the sequence or equal elements remain the same before and aftersorting. This is not the case for unstable sort.
Calls sorting in ascending order by default, only works on types T that implement the comparable<T> interface
Alternatively, you can pass through your own comparison operator, returning
Ordering.LTif left-hand side is less than the right-hand side in orderingOrdering.RTif left-hand side is greater than left-hand side in orderingOrdering.EQif both are equal.
The passed comparison operator, sorts the list in descending order
sorting.cj
import std.sort.*
import std.random.*
main() {
let r: Random = Random()
r.seed = 68
let arr: Array<UInt16> = Array<UInt16>(15, {_ => r.nextUInt16()})
arr.sort(stable: true)
let arr2: Array<UInt16> = Array<UInt16>(15, {_ => r.nextUInt16()})
arr2.sortBy(stable: true) {
rht: UInt16, lht: UInt16 =>
if (rht > lht) {
return Ordering.LT
}
if (rht < lht) {
return Ordering.GT
}
return Ordering.EQ
}
for (x in arr) {
print("${x} ")
}
println()
for (x in arr2) {
print("${x} ")
}
println()
}
// Output:
// 3688 6434 10649 11621 14049 15694 17998 20519 27180 42047 50464 54444 58147 59107 59339
// 58236 55494 46404 44746 38773 35561 32075 28573 26590 26179 22632 12469 9450 2124 1656