unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Stephen Berman <stephen.berman@gmx.net>
To: Jean Louis <bugs@gnu.support>
Cc: 45361@debbugs.gnu.org
Subject: bug#45361: 28.0.50; tabulated-list-mode: should be sorting by specified sort function
Date: Tue, 22 Dec 2020 10:31:35 +0100	[thread overview]
Message-ID: <87v9cu9og8.fsf@gmx.net> (raw)
In-Reply-To: <courier.000000005FE19CB0.000034A7@stw1.rcdrun.com> (Jean Louis's message of "Tue, 22 Dec 2020 08:13:50 +0100")

On Tue, 22 Dec 2020 08:13:50 +0100 Jean Louis <bugs@gnu.support> wrote:

> PROBLEM:
>
> The variable `tabulated-list-format' provides for programmer
> option to sort columns and I would like to sort number as
> strings "12" as numbers, not as strings. I do not know how to
> properly provide the sorting function to `tabulated-list-format' so
> that it works when actually sorting.
[...]
> Then I would like to use the function `string-collate-lessp' as that
> seem to understand how numbers should be compared.
>
> For example this is giving me correct result:
>
> (sort '("121" "117" "1") 'string-collate-lessp) => ("1" "117" "121")

But:

(sort '("21" "117" "1") 'string-collate-lessp) => ("1" "117" "21")

So string-collate-lessp doesn't do what it seems you want.

[...]
> But when I click on "Count" column or press S then it again gets
> sorted rather alphabetic, not by using my function.
>
> That is where I would need help to understand what is happening.
>
> Because when I press S in the column "Count" when cursor is on the number, it invokes this function:
>
> (defun tabulated-list-sort (&optional n)
>   "Sort Tabulated List entries by the column at point.
> With a numeric prefix argument N, sort the Nth column."
>   (interactive "P")
>   (let ((name (if n
> 		  (car (aref tabulated-list-format n))
> 		(get-text-property (point)
> 				   'tabulated-list-column-name))))
>     (if (nth 2 (assoc name (append tabulated-list-format nil)))
>         (tabulated-list--sort-by-column-name name)
>       (user-error "Cannot sort by %s" name))))
>
> Then this one:
>
> (defun tabulated-list--sort-by-column-name (name)
>   (when (and name (derived-mode-p 'tabulated-list-mode))
>     ;; Flip the sort order on a second click.
>     (if (equal name (car tabulated-list-sort-key))
> 	(setcdr tabulated-list-sort-key
> 		(not (cdr tabulated-list-sort-key)))
>       (setq tabulated-list-sort-key (cons name nil)))
>     (tabulated-list-init-header)
>     (tabulated-list-print t)))
>
> And if I read well those functions do not search for my function and
> invoke `my-sort', but they should. Am I right there?

Actually, tabulated-list-print does call my-sort via
tabulated-list--get-sorter.  Anyway, buffer-menu.el has the function
`tabulated-list-entry-size->', which with a small adjustment does what
you seem to want.  Try this:

(defun my-tabulated-list-entry-size-> (entry1 entry2)
  (> (string-to-number (aref (cadr entry1) 0))
     (string-to-number (aref (cadr entry2) 0))))

(let ((buffer "*tabulated*"))
  (switch-to-buffer-other-window buffer)
  (setq tabulated-list-format
	[("Count" 10 my-tabulated-list-entry-size-> :right-align t)
	 ("Name" 60 t)])
  (setq tabulated-list-entries '((6 ["6" "Mercury"])
				 (7 ["7" "GNU Emacs"])
				 (8 ["8" "YouTube Videos"])
				 (9 ["9" "Speedy worker cleaning tables in a restaurant"])
				 (71 ["71" "Trenching"])
				 (82 ["82" "Easier trenching idea"])))
  (tabulated-list-mode)
  (tabulated-list-init-header)
  (hl-line-mode)
  (tabulated-list-print))

Steve Berman





  reply	other threads:[~2020-12-22  9:31 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-22  7:13 bug#45361: 28.0.50; tabulated-list-mode: should be sorting by specified sort function Jean Louis
2020-12-22  9:31 ` Stephen Berman [this message]
2020-12-22  9:43   ` Jean Louis

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=87v9cu9og8.fsf@gmx.net \
    --to=stephen.berman@gmx.net \
    --cc=45361@debbugs.gnu.org \
    --cc=bugs@gnu.support \
    /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.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

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).