Update natural sort for lists #11

Open
aleksi wants to merge 1 commit from aleksi/lib:natural-sort into main
First-time contributor

This commit updates the natural sort for lists to give special
consideration to numbers. Previously, the invocation of
lib.strings.split did not match anything, because the intended regex was
actually escaped by lib.strings.split. Hence we use builtins.split
instead.

Also, the lib.strings.into.int only would consider the first
character of the string and turn it into a char code. This commit
implements another function to parse a string of decimal digits into
an int.

This commit updates the natural sort for lists to give special consideration to numbers. Previously, the invocation of lib.strings.split did not match anything, because the intended regex was actually escaped by lib.strings.split. Hence we use builtins.split instead. Also, the lib.strings.into.int only would consider the first character of the string and turn it into a char code. This commit implements another function to parse a string of decimal digits into an int.
aleksi added 1 commit 2025-10-04 15:47:01 +00:00
This commit updates the natural sort for lists to give special
consideration to numbers. Previously, the invocation of
lib.strings.split did not match anything, because the intended regex was
actually escaped by lib.strings.split. Hence we use builtins.split
instead.

Also, the lib.strings.into.int only would consider the first
character of the string and turn it into a char code. This commit
implements another function to parse a string of decimal digits into
an int.
aleksi reviewed 2025-10-04 15:47:52 +00:00
@ -20,0 +20,4 @@
## Convert from an ASCII representation of an integer to said integer.
##
## @type String -> Integer
parseInt =
Author
First-time contributor

I'm not sure I've chosen the best possible name for this function. I had a hard time coming up with something else.

I'm not sure I've chosen the best possible name for this function. I had a hard time coming up with something else.
Owner

it's a perfectly good name :)

it's a perfectly good name :)
Owner

I think the pattern in lib would have this named: lib.strings.from.int

I think the pattern in lib would have this named: `lib.strings.from.int`
aleksi reviewed 2025-10-04 15:49:13 +00:00
@ -33,0 +41,4 @@
cmpVectors =
a: b:
if a.isNumber && !b.isNumber then
# Numbers come even before special characters in this sorting.
Author
First-time contributor

Might be unnatural?

Might be unnatural?
Owner

I would put numbers at the same place in the collation that digit characters come in ASCII.... after '/' and before ':'. I realize that's awkward because it's halfway through the punctuation, but that way it will agree with what directory listings do, and a lot people name their files based on how ls and similar tools collate things.

I would put numbers at the same place in the collation that digit characters come in ASCII.... after '/' and before ':'. I realize that's awkward because it's halfway through the punctuation, but that way it will agree with what directory listings do, and a lot people name their files based on how `ls` and similar tools collate things.
aleksi reviewed 2025-10-04 15:49:54 +00:00
@ -33,0 +47,4 @@
1
else
# lib.numbers.compare happens to be compatible with both strings
# and integers.
Author
First-time contributor

This compatibility might be unintentional.

This compatibility might be unintentional.
aleksi force-pushed natural-sort from 881275d80d to b620f6581c 2025-10-05 07:27:18 +00:00 Compare
aleksi changed title from WIP: Update natural sort for lists to Update natural sort for lists 2025-10-05 07:27:50 +00:00
Owner

good idea and well implemented; I did leave a suggestion about the semantics above, which imo we should discuss and agree on before merging.

is this code actually used right now? by what? I'd like to be clearer about which listings it would immediately effect, if we were to merge it

good idea and well implemented; I did leave a suggestion about the semantics above, which imo we should discuss and agree on before merging. is this code actually used right now? by what? I'd like to be clearer about which listings it would immediately effect, if we were to merge it
jakehamilton reviewed 2025-10-18 22:29:09 +00:00
@ -39,3 +50,3 @@
value
]) list;
isLess = a: b: (lib.lists.compare lib.numbers.compare (builtins.head a) (builtins.head b)) < 0;
cmpVectors =
Owner

It is a small thing, but can we write out compare instead of cmp?

It is a small thing, but can we write out `compare` instead of `cmp`?
Owner

oh, yes, seconded, good call

oh, yes, seconded, good call
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u natural-sort:aleksi-natural-sort
git checkout aleksi-natural-sort

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git checkout main
git merge --no-ff aleksi-natural-sort
git checkout aleksi-natural-sort
git rebase main
git checkout main
git merge --ff-only aleksi-natural-sort
git checkout aleksi-natural-sort
git rebase main
git checkout main
git merge --no-ff aleksi-natural-sort
git checkout main
git merge --squash aleksi-natural-sort
git checkout main
git merge --ff-only aleksi-natural-sort
git checkout main
git merge aleksi-natural-sort
git push origin main
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
3 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: auxolotl/lib#11
No description provided.