2

I have a list of lists of this form :

testlist((a 22) ( b 2) ( c 5))

My goal is to sort the testlist based on the numbers' values (5 first greatest values). Is there a way to achieve this without having to iterate over the list and compare numbers to each other?

1 Answer 1

5

In ANSI Common Lisp:

(sort (copy-list '((a 22) (b 2) (c 5)))  #'< :key #'second)
-> ((B 2) (C 5) (A 22))

The copy-list is there because sort clobbers the input list. We need that because our input list is a literal, but even if it isn't, sometimes you don't want to scramble the original object that you wish to sort.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.