Re: stable sort_by?

… So, in this case [1,“c”] and [1,“b”] are equivalent, and
their order
is un-important …

From Sorting algorithm - Wikipedia :

Stability: stable sorting algorithms maintain the relative order
of records with equal keys (i.e. values). That is, a sorting algorithm
is stable if whenever there are two records R and S with the same
key and with R appearing before S in the original list, R will appear
before S in the sorted list.

cheers

Simon

Kroeger, Simon (ext) wrote:

… So, in this case [1,“c”] and [1,“b”] are equivalent, and
their order
is un-important …

From Sorting algorithm - Wikipedia :

Stability: stable sorting algorithms maintain the relative order
of records with equal keys (i.e. values). That is, a sorting algorithm
is stable if whenever there are two records R and S with the same
key and with R appearing before S in the original list, R will appear
before S in the sorted list.

It’s pretty easy to transform every sorting operation into a stable one:

orig:
enum.sort_by {|x| calculate_key(x)}

stable:
i=0
enum.sort_by {|x| [calculate_key(x), i+=1]}

Kind regards

robert