Search code examples
directorycommon-lispalphabeticalclispalphabetical-sort

Is there a easy way to get the "directory" function to put the values in alphabetical order?


Retrieving all paths for the files with .pas ending in my subfolder "testfiles" with the directory function. So far so good. The problem is that the paths is not in alphabetical order.

The paths in the list is not strings or list, so the sort function does not work for me either.

Is there a easy way to fix this?

Code:

(setq test (directory "testfiles/*.pas"))

Solution

  • The sequence functions in Common Lisp are powerful, through the various keyword parameters (:key, :test, etc., depending on the function). SORT takes an optional :key parameter.

    (sort (directory "testfiles/*.pas") #'string< :key #'pathname-name)
    

    Note: SORT is a destructive operation. But this is not a problem here, because DIRECTORY always returns a freshly consed-up list.