[racket-dev] Feature request: multiple keys in sort

From: Danny Yoo (dyoo at hashcollision.org)
Date: Sun Jun 10 16:52:46 EDT 2012

>> Would it be possible to extend the sort function to allow for multiple
>> keys to facilitate sorting lists of lists or lists of structs.

The sort function should accommodate this by providing a custom
comparator as the second (optional) argument to it.

When building custom comparators, you might find the "datum-order"
function from the data/order library to be helpful, as its definition
knows how to deal with lists of comparable things:


For example:

#lang racket
(require data/order)

(struct person (name age) #:transparent)
(define p1 (person "Danny" 33))
(define p2 (person "Grandma" 94))
(define p3 (person "Grandma" 92))

(define (my-less-than x y)
  (eq? '< (datum-order x y)))

(sort (list p1 p2 p3) my-less-than)

It's a little unfortunate that there's a slight impedance mismatch
between what datum-order provides and what sort expects; the
my-less-than function in the example adapts the output of datum-order
so it can be used with sort.

Posted on the dev mailing list.