DEV Community


Posted on

The case against "natural/localized" sorting

So my favourite text editor on the iPad updated -- Textastic went version 8, and one of the features is choosable sorting, by name, extension, date, and size. All fine and dandy, I thought.

I then went to find a file... and it wasn't where I was expecting it.

Here I am, on a commuter train going to Baltimore, pulling up my phone and franticly searching for a file I knew I created and named. Not in Textastic. Not in Dropbox, where it's synced. Not in the regular places...

I then relaunch Textastic... and find it... in the wrong spot. Same directory -- they changed how it's sorted.

It's only ASCII! It's so simple!

Let me put this into context. I'm writing a novel, mixing a weird bit of furry culture with the Station to Station nomadic train, called Throng: Going Station to Station. PLUG WARNING You can see the story of a multi-furry band tour by train at my Patreon PLUG WARNING.

The novel is written in sections, each between 5-10K in size. Chapters are at most 8 sections long, so to keep them in some order, I name them with a section number prefix... that's in hexadecimal.

Here's a small example:

As the story progresses through various cities along existing real life rail lines, I'm naming the sections after them.

So what does GTK's file picker, MacOS Finder, and Textastic do? It treats the first number as that, a number, and ignores any front-facing zeros. This is so you can have this:

file 8
file 9
file 10
file 11
file 12

Okay, that's fine... but what about my naming convention? Thankfully, Linux's "ls" requires you to give it the "-v" switch.

$ ls -v1 *.md

And that's what I'm seeing in MacOS, in GTK's file picker, and now Textastic.

Why hate on strcmp?

It's UTF-8, aka Unicode. I'm running the "en_US.utf8" localization, mainly because it's default. MacOS and iOS uses it. GTK uses it. And now Textastic picks it up, because it's using iOS' NSString.localizedStandardCompare routine... and it sorts by context instead of byte-wise. And it's mandated by Unicode's spec.

That's the thing. It may be mandated but it's not right in all cases. I have the breaking case.

What can be done?

As a programmer, give users the option to turn off "natural" sorting for "literal" sorting. The more options, the better.

As a user, file bugs. I did so for Textastic and GTK/XFCE.

Resist the urge to work-around. Linux has it right at the command line -- make it optional.

Top comments (0)