help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Is there a function to sort file names by file version number, like


From: Sean McAfee
Subject: Re: Is there a function to sort file names by file version number, like "ls -v"?
Date: Thu, 23 May 2013 11:33:01 -0700
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.1 (gnu/linux)

florian@fsavigny.de (Florian v. Savigny) writes:
> I am wondering if there is somehow a comparison function (of the same
> category as string-lessp) in Elisp which compares file name strings by
> these version numbers, so I could sort the result of
> directory-file-names in the same way as "ls -v".

I don't think so, but it's easy to write one:

(defun versioned-filename-lessp (a b)
  (labels ((parse (str)
             (string-match "\\(?:~\\([0-9]+\\)~\\)?\\'" str)
             (cons (substring str 0 (match-beginning 0))
                   (and (match-beginning 1)
                        (string-to-number (match-string 1 str))))))
    (save-match-data
      (let ((ap (parse a))
            (bp (parse b)))
        (or (string-lessp (car ap) (car bp))
            (and (not (string-lessp (car bp) (car ap)))
                 (cdr ap)
                 (cdr bp)
                 (< (cdr ap) (cdr bp))))))))


reply via email to

[Prev in Thread] Current Thread [Next in Thread]