From: Tassilo Horn <tsdh@gnu.org>
To: Rasmus <rasmus@gmx.us>
Cc: help-gnu-emacs@gnu.org
Subject: Re: Sorting buffer with string-collate-lessp
Date: Tue, 26 May 2015 16:49:27 +0200 [thread overview]
Message-ID: <87twuz1jig.fsf@gnu.org> (raw)
In-Reply-To: <87egm34kt0.fsf@gmx.us> (rasmus@gmx.us's message of "Tue, 26 May 2015 13:53:15 +0200")
Rasmus <rasmus@gmx.us> writes:
Hi Rasmus,
> How can I easily sort a buffer using string-collate-lessp?
>
> Info: I would like to sort a buffer using string-collate-lessp (line
> by line). Sort-lines is the obvious candidate but it uses string<. I
> tried to write my own sort-lines using sort-subr, as it has a
> predicate argument. However, for buffers, it needs something like
> compare-buffer-substrings, which takes no predicate and is in the
> C-level and pretty long.
>
> I could write a wrapper that convert each buffer-chunk into its
> buffer-substring first, and then compares it with
> string-collate-lessp, I guess, but that seems like a lot of boiler
> plate. So maybe a better solution exists?
I think you can use `cl-left' to temporarily change the definition of
`string<' to `string-collate-lessp', so this should work in theory.
--8<---------------cut here---------------start------------->8---
(cl-letf (((symbol-function 'string<) #'string-collate-lessp))
(sort-lines nil (point-min) (point-max)))
--8<---------------cut here---------------end--------------->8---
However, I've tried it with (lambda (a b) (string-collate-lessp b a))
which should use `string-collate-lessp' and sort in reverse (note the
switched arguments) and that didn't change anything.
So either my `cl-letf' usage is wrong or `sort-lines' doesn't really use
`string<'. (Actually, `string<' is an alias to `string-lessp', so I
also tried changing that accordingly, but still no effect...)
Bye,
Tassilo
next prev parent reply other threads:[~2015-05-26 14:49 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-05-26 11:53 Sorting buffer with string-collate-lessp Rasmus
2015-05-26 14:49 ` Tassilo Horn [this message]
2015-05-26 14:53 ` Rasmus
2015-05-26 17:57 ` Tassilo Horn
2015-05-27 11:23 ` Michael Heerdegen
[not found] <mailman.3682.1432641217.904.help-gnu-emacs@gnu.org>
2015-05-26 15:37 ` Emanuel Berg
2015-05-26 15:43 ` Rasmus
2015-05-26 15:52 ` Rasmus
[not found] ` <mailman.3702.1432655548.904.help-gnu-emacs@gnu.org>
2015-05-26 16:06 ` Emanuel Berg
2015-05-26 16:36 ` Rasmus
2015-05-26 16:40 ` Rasmus
2015-05-26 18:01 ` Tassilo Horn
[not found] ` <mailman.3706.1432658186.904.help-gnu-emacs@gnu.org>
2015-05-26 22:44 ` Emanuel Berg
2015-05-27 0:12 ` Rasmus
[not found] ` <mailman.3732.1432685593.904.help-gnu-emacs@gnu.org>
2015-05-27 0:25 ` Emanuel Berg
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87twuz1jig.fsf@gnu.org \
--to=tsdh@gnu.org \
--cc=help-gnu-emacs@gnu.org \
--cc=rasmus@gmx.us \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).