all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Need help with sort function for tabulated-list-format
@ 2022-08-26 17:28 Jean Louis
  2022-08-26 17:50 ` [SOLVED] " Jean Louis
  0 siblings, 1 reply; 6+ messages in thread
From: Jean Louis @ 2022-08-26 17:28 UTC (permalink / raw)
  To: Help GNU Emacs

In my extensive tabulated list work, usually I use the ID number as
the first column. The ID number, while number in reality, it is
string.

That would mean that I would need to make a function that converts
strings to numbers before sorting such.

And here below there is explanation that SORT shall represent
predicate to `sort' function.

 -- Variable: tabulated-list-format
     This buffer-local variable specifies the format of the Tabulated
     List data.  Its value should be a vector.  Each element of the
     vector represents a data column, and should be a list ‘(NAME WIDTH
     SORT)’, where

        • NAME is the column’s name (a string).

        • WIDTH is the width to reserve for the column (an integer).
          This is meaningless for the last column, which runs to the end
          of each line.

        • SORT specifies how to sort entries by the column.  If ‘nil’,
          the column cannot be used for sorting.  If ‘t’, the column is
          sorted by comparing string values.  Otherwise, this should be
          a predicate function for ‘sort’ (*note Rearrangement::), which
          accepts two arguments with the same form as the elements of
          ‘tabulated-list-entries’ (see below).


Here is my tabulated-list-format:

(defvar cf-people-tabulated-format-with-account [("ID" 8 t) ("Name" 40 t) ("Account" 40 t)])

I would like to provide the mentioned predicate here, something like:

(defvar cf-people-tabulated-format-with-account [("ID" 8 string-as-number-is-smaller-than) ("Name" 40 t) ("Account" 40 t)])

Purpose is to sort the first column by its number represented as string.

 320193   Qg
 328635   Sgh
 334546   Tbrich
 338532   jnet
 347050   ygmail.com
 347886   Tsimmons
 360982   mail.com
 361677   D

Does anybody have sample predicate function for `sort' which accepts
two arguments as the elements of `tabulated-list-entries'?



Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

In support of Richard M. Stallman
https://stallmansupport.org/



^ permalink raw reply	[flat|nested] 6+ messages in thread

* [SOLVED] Re: Need help with sort function for tabulated-list-format
  2022-08-26 17:28 Need help with sort function for tabulated-list-format Jean Louis
@ 2022-08-26 17:50 ` Jean Louis
  2022-08-26 19:42   ` Emanuel Berg
  0 siblings, 1 reply; 6+ messages in thread
From: Jean Louis @ 2022-08-26 17:50 UTC (permalink / raw)
  To: Help GNU Emacs

I guess I solved it by looking into the package package.

(defun rcd-number-as-string-predicate (A B)
  "Predicate to sort numbers in columns of strings."
  (let ((vA (string-to-number (aref (cadr A) 0)))
        (vB (string-to-number (aref (cadr B) 0))))
    ;;(message-box "%s" vA)
    (< vA vB)))

And now this works well, I can sort columns containing numbers as
I guess I solved it by looking into package packagestrings:

(defvar cf-people-tabulated-format-with-account [("ID" 8 rcd-number-as-string-predicate) ("Name" 40 t) ("Account" 40 t)])

-- 
Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

In support of Richard M. Stallman
https://stallmansupport.org/



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [SOLVED] Re: Need help with sort function for tabulated-list-format
  2022-08-26 17:50 ` [SOLVED] " Jean Louis
@ 2022-08-26 19:42   ` Emanuel Berg
  2022-08-27  3:39     ` Jean Louis
  0 siblings, 1 reply; 6+ messages in thread
From: Emanuel Berg @ 2022-08-26 19:42 UTC (permalink / raw)
  To: help-gnu-emacs

Jean Louis wrote:

> I guess I solved it by looking into the package package

Sort the numeric `sort-numeric-fields' fields.

-- 
underground experts united
https://dataswamp.org/~incal




^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [SOLVED] Re: Need help with sort function for tabulated-list-format
  2022-08-26 19:42   ` Emanuel Berg
@ 2022-08-27  3:39     ` Jean Louis
  2022-08-27  4:22       ` Emanuel Berg
  0 siblings, 1 reply; 6+ messages in thread
From: Jean Louis @ 2022-08-27  3:39 UTC (permalink / raw)
  To: help-gnu-emacs

* Emanuel Berg <incal@dataswamp.org> [2022-08-27 06:31]:
> Jean Louis wrote:
> 
> > I guess I solved it by looking into the package package
> 
> Sort the numeric `sort-numeric-fields' fields.

That function is for sorting lines in region numerically.

While I had to find a function to sort a column within the tabulated
list mode.

Sample value of `tabulated-list-entries':

Value:
((1
  ["1" ""])
 (2
  ["2" "ACTION"])
 (3
  ["3" "COMPLETED"])
 (4
  ["4" "PENDING"])
 (5
  ["5" "UPDATE PENDING"])
 (6
  ["6" "DECISION REVERSED"]))

That means that this first column with "1", "2", that one has to be
used for sorting. By sorting it as strings, then it goes like "1" "10"

 1          
 2          ACTION
 3          COMPLETED
 4          PENDING
 5          UPDATE PENDING
 6          DECISION REVERSED

 6          DECISION REVERSED
 5          UPDATE PENDING
 4          PENDING
 3          COMPLETED
 2          ACTION
 1          

Sorting as strings:

(sort '("100" "9" "10" "1") 'string<) ⇒ ("1" "10" "100" "9")

That would be incorrect for numbers, rightß

Tabulated list mode by default sorts columns by its strings.

So I had to make a function that converts strings to numbers, so that
we get something like this:

(sort '(100 9 10 1) '<) ⇒ (1 9 10 100)

Which then makes sense.



-- 
Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

In support of Richard M. Stallman
https://stallmansupport.org/



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [SOLVED] Re: Need help with sort function for tabulated-list-format
  2022-08-27  3:39     ` Jean Louis
@ 2022-08-27  4:22       ` Emanuel Berg
  2022-08-29  8:01         ` Jean Louis
  0 siblings, 1 reply; 6+ messages in thread
From: Emanuel Berg @ 2022-08-27  4:22 UTC (permalink / raw)
  To: help-gnu-emacs

Jean Louis wrote:

> So I had to make a function that converts strings to numbers

Such a function already exists, `string-to-number' but ... why
are integers stored in and as strings to begin with?

-- 
underground experts united
https://dataswamp.org/~incal




^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [SOLVED] Re: Need help with sort function for tabulated-list-format
  2022-08-27  4:22       ` Emanuel Berg
@ 2022-08-29  8:01         ` Jean Louis
  0 siblings, 0 replies; 6+ messages in thread
From: Jean Louis @ 2022-08-29  8:01 UTC (permalink / raw)
  To: Emanuel Berg, help-gnu-emacs

Let me elaborate, it was about converting strings to numbers and during tabulated list mode by those converted numbers.

Predicate function was needed.

Did you review it?


On August 27, 2022 4:22:48 AM UTC, Emanuel Berg <incal@dataswamp.org> wrote:
>Jean Louis wrote:
>
>> So I had to make a function that converts strings to numbers
>
>Such a function already exists, `string-to-number' but ... why
>are integers stored in and as strings to begin with?


Jean



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2022-08-29  8:01 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-08-26 17:28 Need help with sort function for tabulated-list-format Jean Louis
2022-08-26 17:50 ` [SOLVED] " Jean Louis
2022-08-26 19:42   ` Emanuel Berg
2022-08-27  3:39     ` Jean Louis
2022-08-27  4:22       ` Emanuel Berg
2022-08-29  8:01         ` Jean Louis

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.