I am a newbie in Lisp and I want to learn Lisp programming.
I want to sort some lists read from a text file, like in the following form:
(a 120 135 124 124)
(b 120 135 124 124)
(c 120 135 124 124)
What is the best way to sort them according to the first integer element or maybe second or third and so on?
I have the following idea:
- read them all and put them into a list of lists
- iterate over the container list and compare the values of list with following one like in bubble sort.
Are there more suitable data structures to achieve this, maybe like Collections in Java which take comparable objects that contain sort logic and fullfill sorting automatically?
Thank you very much.
The standard
sortfunction takes a:keyargument that can be used to extract a value from the object to use as the sort key. For your example, if you had each list from the file in a list calledobjects, the following would destructively sortobjectsby the first integer element and return a sorted list:See http://l1sp.org/cl/sort for the precise specification of Common Lisp’s
sortfunction.