From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Tassilo Horn Newsgroups: gmane.emacs.help Subject: Re: Sorting buffer with string-collate-lessp Date: Tue, 26 May 2015 16:49:27 +0200 Message-ID: <87twuz1jig.fsf@gnu.org> References: <87egm34kt0.fsf@gmx.us> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: ger.gmane.org 1432651801 27430 80.91.229.3 (26 May 2015 14:50:01 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 26 May 2015 14:50:01 +0000 (UTC) Cc: help-gnu-emacs@gnu.org To: Rasmus Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Tue May 26 16:49:52 2015 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1YxGAw-0000Bd-O4 for geh-help-gnu-emacs@m.gmane.org; Tue, 26 May 2015 16:49:50 +0200 Original-Received: from localhost ([::1]:48427 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YxGAv-0001KF-VP for geh-help-gnu-emacs@m.gmane.org; Tue, 26 May 2015 10:49:49 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:59439) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YxGAf-0001AH-M2 for help-gnu-emacs@gnu.org; Tue, 26 May 2015 10:49:34 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1YxGAb-0002PL-Kr for help-gnu-emacs@gnu.org; Tue, 26 May 2015 10:49:33 -0400 Original-Received: from deliver.uni-koblenz.de ([141.26.64.15]:53084) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YxGAb-0002P5-DH for help-gnu-emacs@gnu.org; Tue, 26 May 2015 10:49:29 -0400 Original-Received: from thinkpad-t440p (dhcp77.uni-koblenz.de [141.26.71.77]) (using TLSv1 with cipher AES128-SHA (128/128 bits)) (No client certificate requested) by deliver.uni-koblenz.de (Postfix) with ESMTPSA id 41E311A82E4; Tue, 26 May 2015 16:49:27 +0200 (CEST) Mail-Followup-To: Rasmus , help-gnu-emacs@gnu.org In-Reply-To: <87egm34kt0.fsf@gmx.us> (rasmus@gmx.us's message of "Tue, 26 May 2015 13:53:15 +0200") User-Agent: Gnus/5.130014 (Ma Gnus v0.14) Emacs/25.0.50 (gnu/linux) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6.x X-Received-From: 141.26.64.15 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:104577 Archived-At: Rasmus 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