Sorting according to a primary and secondary criterion

Joseph Rushton Wakeling joseph.wakeling at webdrake.net
Wed Jul 17 06:12:08 PDT 2013


On 07/17/2013 02:42 PM, bearophile wrote:
> You need a lambda delegate for that. But I forgot about multisort algorithm...
> It's probably the right tool.

So, in the end I tried out 3 different alternatives:

  schwartzSort(a => tuple(arr1[a], arr2[a]), "a < b")
  sort((a, b) => arr1[a] < arr1[b] || (arr1[a] == arr1[b] && arr2[a] < arr2[b]))
  multiSort((a, b) => arr1[a] < arr1[b], (a, b) => arr2[a] < arr2[b])

sort() seems faster than schwartzSort for smaller-scale stuff, but takes longer
for larger-scale sorts.  multiSort wins out over both of them.

I guess in principle it might be possible to have a multiSchwartzSort which
allows for multiple transformations:

  multiSchwartzSort(a => arr1[a], a => arr2[a], "a < b", "a < b")

... which might gain some speed by caching the results of the transformations,
as schwartzSort is supposed to do?

But anyway, I think this solution works at limited extra cost speed-wise. :-)

Thanks very much to both of you!



More information about the Digitalmars-d-learn mailing list