I'm trying to sort a list that looks something like this:
(defvar my-list '((:x 1 :y something) (:x 5 :y something) (:x 19 :y something)))
I'm trying to sort it by the value in :x. I could do that like this
(sort my-list #'> :key #'second)
but I would very much prefer to use the getf function instead of second, but I can't figure out how to pass :x as a parameter.
From what I can gather just #'getf returns (getf ((:x 1 :y something) '(:x 5 :y something) (:x 19 :y something)) [external]. How would I go about passing :x as the second parameter?
The only way I could think of is to create a wrapper-function for getf, which only takes a list as a parameter and passes in :x by default. There must be a better way though.
SORTon quoted (literal) lists. You can useCOPY-LIST(orCOPY-TREE) to make a copy of the list before sorting. For the problem itself, making a wrapper function is the usual solution. That is commonly known as currying. The Alexandria library has functionsCURRYandRCURRYfor it.(:type list)), in which case you can use the accessor for the slot.(sort *my-list* #'> :key ^(getf % :x)).