Exa: Add option to disable natural sorting

Created on 19 Sep 2017  路  13Comments  路  Source: ogham/exa

So with ls I get:

[svenstaro:/tmp/thing] % ls -lha
total 89M
drwxr-xr-x  7 svenstaro svenstaro  160 Sep 19 10:18 ./
drwxrwxrwt 25 root      root      1.3K Sep 19 10:18 ../
-rw-r--r--  1 svenstaro svenstaro  89M Sep 19 10:17 20151104-172114.nav
drwxr-xr-x  4 svenstaro svenstaro  120 Sep 19 10:17 20151104-172114_TOW322807-322854.las_converted/
drwxr-xr-x  4 svenstaro svenstaro  120 Sep 19 10:17 20151104-172114_TOW322814-324313.las_converted/
drwxr-xr-x  2 svenstaro svenstaro   40 Sep 19 10:17 36/
drwxr--r--  5 svenstaro svenstaro  28K Sep 19 10:17 366be5b7-3d54-469a-bf75-79dd7da29207.cesium/
drwxr--r--  4 svenstaro svenstaro  42K Sep 19 10:17 366be5b7-3d54-469a-bf75-79dd7da29207.entwine/

and with exa I get:

[svenstaro:/tmp/thing] % exa -l
drwxr-xr-x   - svenstaro 19 Sep 10:17 36
drwxr--r--   - svenstaro 19 Sep 10:17 366be5b7-3d54-469a-bf75-79dd7da29207.cesium
drwxr--r--   - svenstaro 19 Sep 10:17 366be5b7-3d54-469a-bf75-79dd7da29207.entwine
.rw-r--r-- 92M svenstaro 19 Sep 10:17 20151104-172114.nav
drwxr-xr-x   - svenstaro 19 Sep 10:17 20151104-172114_TOW322807-322854.las_converted
drwxr-xr-x   - svenstaro 19 Sep 10:17 20151104-172114_TOW322814-324313.las_converted

I think this is weird.

Why does exa sort it this way? I believe exa defaults to name ordering but then this doesn't make any sense. In fact if I make it order by name explicitly, I get the same result. Why would it order the 36 up there when a 2015... exists?

This is exa 0.7.

Most helpful comment

My guess is that it is sorted by the numeric value of the first group of digits:

16 < 26 < 36 < 46 < 366 < 20151104

But until somebody reads the code or has read it already and responds to this, the real answer will remain a mistery ...

All 13 comments

This got me curious, so I added a few more files and here's what I'm seeing:

.rw-r--r--  0 jsd 19 Sep 11:45 16
.rw-r--r--  0 jsd 19 Sep 11:45 26
.rw-r--r--  0 jsd 19 Sep 11:44 36
.rw-r--r--  0 jsd 19 Sep 11:45 46
.rw-r--r--  0 jsd 19 Sep 11:44 366be5b7-3d54-469a-bf75-79dd7da29207.cesium
.rw-r--r--  0 jsd 19 Sep 11:44 366be5b7-3d54-469a-bf75-79dd7da29207.entwine
.rw-r--r--  0 jsd 19 Sep 11:44 20151104-172114.nav
.rw-r--r--  0 jsd 19 Sep 11:44 20151104-172114_TOW322807-322854.las_converted
.rw-r--r--  0 jsd 19 Sep 11:44 20151104-172114_TOW322814-324313.las_converted

I'm guessing it's grouping files by type, and the ones with no extension are listed first?

Is this expected behavior? Can I change it to behave like ls?

My guess is that it is sorted by the numeric value of the first group of digits:

16 < 26 < 36 < 46 < 366 < 20151104

But until somebody reads the code or has read it already and responds to this, the real answer will remain a mistery ...

Looks like @FSMaxB is right! Here's the comment from the code explaining this behavior:
https://github.com/ogham/exa/blob/f55bd6de5351007325df504f9efdf1d7183059d5/src/fs/filter.rs#L214-L218

Mh I think a sort option for "stupid" by name sorting would be useful then?

Yeah, that's correct: it sorts numbers as numbers, and non-numbers as strings. (Well, the rust-natord crate does all the work)

I could add an option to disable this behaviour if there's interest.

There's interest from me at least @ogham. I think there is merit in having a ls-compatible option.

I'll add an option then!

In the meantime, feel free to bikeshed what this option should be called :) "un-natural sorting" sounds weird, right?

How about "canonical sorting"? Sounds ambiguous enough

@hoodie I like it

Any status with this? Been creating reproducible tools and as a result been messing with disorderfs, and i kept wondering why I never saw anything when doing the mount. Turns out since exa has no way to actually turn off sorting, I can't use exa to verify the disorderfs mount!

位 disorder 禄 ls
reversed  rootdir  shuffle  sorted
位 disorder 禄 /usr/bin/ls -f rootdir
.  ..  a  b  c
位 disorder 禄 /usr/bin/ls -f reversed 
c  b  a  ..  .
位 disorder 禄 /usr/bin/ls -f sorted  
.  ..  a  b  c
位 disorder 禄 exa reversed
a  b  c
位 disorder 禄 exa sorted  
a  b  c
位 disorder 禄 exa shuffle
a  b  c
位 disorder 禄 exa shuffle
a  b  c
位 disorder 禄 /usr/bin/ls -f shuffle
.  b  ..  c  a
位 disorder 禄 /usr/bin/ls -f shuffle
.  c  a  ..  b

https://github.com/ReproducibleBuilds/disorderfs

@Foxboron You can pass --sort=none to disable sorting. It'll just list files in the order they get read. When I added the option I wasn't even sure if there was a use for it, but now I know!

I understand the idea behind ordering file10 after file9, and it fits my expectation and intuition.

If the numeric value is in the beginning of the filename however, it somehow breaks my intuition. Take for example this:

88d20e88-d085-4e1b-a416-55cb6ad4b882/
88d20e88-d085-4e1b-a416-55cb6ad4b882.dat
6002c025-cf82-46a2-896f-5d4b143a96b5/
6002c025-cf82-46a2-896f-5d4b143a96b5.dat
a2cdbc16-5ed9-41a9-9c00-1fa9d21ae1d9/
a2cdbc16-5ed9-41a9-9c00-1fa9d21ae1d9.dat
b25d2ae8-e3c1-4917-b4d3-fbb3c32f409a/
b25d2ae8-e3c1-4917-b4d3-fbb3c32f409a.dat

According to what @FSMaxB discoverd, 88 < 6002, and hence 88d20e88-d085-4e1b-a416-55cb6ad4b882 before 6002c025-cf82-46a2-896f-5d4b143a96b5. However, in a string like this the number 6002 does not carry any semantic meaning and is also not easily identified as such. It hence breaks the expectation to have the string containing it at the beginning to come after one starting with 8.

I don't actually know a fix for this. Treating a string differently if a number is in the beginning doesn't quite make sense, because that actually runs into issues with e.g. 9.dat and 10.dat.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

huxi picture huxi  路  3Comments

raxod502 picture raxod502  路  5Comments

grigorii-horos picture grigorii-horos  路  4Comments

MohamedElm1678 picture MohamedElm1678  路  3Comments

simnalamburt picture simnalamburt  路  6Comments