* Sorting directory-files on their extension
@ 2009-10-07 14:35 Nordlöw
2009-10-07 15:11 ` Nordlöw
` (3 more replies)
0 siblings, 4 replies; 6+ messages in thread
From: Nordlöw @ 2009-10-07 14:35 UTC (permalink / raw)
To: help-gnu-emacs
Does Emacs contain any built-in features for sorting file-names (list
of strings) on their (file) extension?
/NOrdlöw
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Sorting directory-files on their extension
2009-10-07 14:35 Sorting directory-files on their extension Nordlöw
@ 2009-10-07 15:11 ` Nordlöw
2009-10-07 15:17 ` Pascal J. Bourguignon
2009-10-07 15:12 ` Pascal J. Bourguignon
` (2 subsequent siblings)
3 siblings, 1 reply; 6+ messages in thread
From: Nordlöw @ 2009-10-07 15:11 UTC (permalink / raw)
To: help-gnu-emacs
On Oct 7, 4:35 pm, Nordlöw <per.nord...@gmail.com> wrote:
> Does Emacs contain any built-in features for sorting file-names (list
> of strings) on their (file) extension?
>
> /NOrdlöw
Is this a good candidate?:
(defun compare-extensions (a b)
(compare-strings
(file-name-extension a) 0 (length (file-name-extension a))
(file-name-extension b) 0 (length (file-name-extension b))))
;; Use: (sort '("a.c" "a.h" "b.c" "b.h") 'compare-extensions)
/Nordlöw
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Sorting directory-files on their extension
2009-10-07 14:35 Sorting directory-files on their extension Nordlöw
2009-10-07 15:11 ` Nordlöw
@ 2009-10-07 15:12 ` Pascal J. Bourguignon
2009-10-07 15:59 ` Harry Putnam
2009-10-07 16:04 ` Harry Putnam
3 siblings, 0 replies; 6+ messages in thread
From: Pascal J. Bourguignon @ 2009-10-07 15:12 UTC (permalink / raw)
To: help-gnu-emacs
Nordlöw <per.nordlow@gmail.com> writes:
> Does Emacs contain any built-in features for sorting file-names (list
> of strings) on their (file) extension?
Yes. Emacs contains a built-in emacs lisp processor, which is able to
compute anything that is computable.
In this case:
(sort file-list (lambda (a b)
(string< (file-name-extension a)
(file-name-extension b))))
--
__Pascal Bourguignon__
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Sorting directory-files on their extension
2009-10-07 15:11 ` Nordlöw
@ 2009-10-07 15:17 ` Pascal J. Bourguignon
0 siblings, 0 replies; 6+ messages in thread
From: Pascal J. Bourguignon @ 2009-10-07 15:17 UTC (permalink / raw)
To: help-gnu-emacs
Nordlöw <per.nordlow@gmail.com> writes:
> On Oct 7, 4:35 pm, Nordlöw <per.nord...@gmail.com> wrote:
>> Does Emacs contain any built-in features for sorting file-names (list
>> of strings) on their (file) extension?
>>
>> /NOrdlöw
>
> Is this a good candidate?:
>
> (defun compare-extensions (a b)
> (compare-strings
> (file-name-extension a) 0 (length (file-name-extension a))
> (file-name-extension b) 0 (length (file-name-extension b))))
> ;; Use: (sort '("a.c" "a.h" "b.c" "b.h") 'compare-extensions)
No, it doesn't work because compare-strings always return true, while
sort expects an order function that returns true only when a<b.
(string< (file-name-extension a) (file-name-extension b))
is what you'd want. If you want it case insignificant, you can use
(string< (upcase (file-name-extension a))
(upcase (file-name-extension b)))
--
__Pascal Bourguignon__
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Sorting directory-files on their extension
2009-10-07 14:35 Sorting directory-files on their extension Nordlöw
2009-10-07 15:11 ` Nordlöw
2009-10-07 15:12 ` Pascal J. Bourguignon
@ 2009-10-07 15:59 ` Harry Putnam
2009-10-07 16:04 ` Harry Putnam
3 siblings, 0 replies; 6+ messages in thread
From: Harry Putnam @ 2009-10-07 15:59 UTC (permalink / raw)
To: help-gnu-emacs
Nordlöw <per.nordlow@gmail.com> writes:
> Does Emacs contain any built-in features for sorting file-names (list
> of strings) on their (file) extension?
Someone here will have better help sooner or later but to get started.
Investigate the `sort-lines' command like this:
M-x apropos <RET> sort-lines <RET>
In the resulting buffer click or press enter on the command shown to
get the full documentation string.
sort-lines is an interactive compiled Lisp function in `sort.el'.
(sort-lines REVERSE BEG END)
,----
| Sort lines in region alphabetically; argument means descending order.
| Called from a program, there are three arguments:
| REVERSE (non-nil means reverse order), BEG and END (region to sort).
| The variable `sort-fold-case' determines whether alphabetic case affects
| the sort order.
`----
It looks like that BEG and END is indicating you can set the region to
sort on... I'm not sure if that means you can set it to the extension but
there is a start.
Another whole approach would be to call the external sort command
(assuming a unix or unix like OS) it has enough flags to set about
anything to sort on.
You can call an external command with `shell-command-on-region'
C+Shift+\
It will give you a chance to setup the flags to sort at a command line.
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Sorting directory-files on their extension
2009-10-07 14:35 Sorting directory-files on their extension Nordlöw
` (2 preceding siblings ...)
2009-10-07 15:59 ` Harry Putnam
@ 2009-10-07 16:04 ` Harry Putnam
3 siblings, 0 replies; 6+ messages in thread
From: Harry Putnam @ 2009-10-07 16:04 UTC (permalink / raw)
To: help-gnu-emacs
Nordlöw <per.nordlow@gmail.com> writes:
> Does Emacs contain any built-in features for sorting file-names (list
> of strings) on their (file) extension?
Sorry about my first respons... I missed the part about this being a
dired kind of operation.
You'd need to use `wdired-mode' or from a dired buffer
`wdired-change-to-wdired-mode'
Which allows you to operate on a directory like it was a file... edit
etc then save.. will effect the directory on disk.
sort-lines may be useful there.
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2009-10-07 16:04 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-10-07 14:35 Sorting directory-files on their extension Nordlöw
2009-10-07 15:11 ` Nordlöw
2009-10-07 15:17 ` Pascal J. Bourguignon
2009-10-07 15:12 ` Pascal J. Bourguignon
2009-10-07 15:59 ` Harry Putnam
2009-10-07 16:04 ` Harry Putnam
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.