[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
bug#45361: 28.0.50; tabulated-list-mode: should be sorting by specified
From: |
Stephen Berman |
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 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux) |
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