I'm trying to write some function to sort this type of list:
((1 A) (2 E) (4 D))
I found the built-in function sort in Common Lisp, but I can't do what I want.
This should be the result:
'((1 A) (4 D) (2 E))
I want to sort the list by the second element of each lists, alphabetically.
This is what I've done:
(sort '((1 A) (4 D) (2 E)) #'char-lessp :key #'second)
Anyway, I would understand how to sort a list with a specific function, using :key'.
Thank you.
You should use:
The reason is that
char-lessp
compares characters, andA
,E
andD
are not characters, but symbols. The functionstring<
can used to compare their names, which are the strings"A"
,"E"
and"D"
. For instance:Note that the comparison operators on strings have two versions, for case sensitive and case insensitive comparisons: