Natural ordering for file names

Recently I found out you cannot just simply sort file names with std.ascii.lessThanIgnoreCase. One would expect a natural sort order.

That means instead of

a11
a2

you get

a2
a11

I didn’t find any Zig implementations, so here’s a port of the C implementation in Zig https://codeberg.org/knightpp/natural-ordering-zig.

But there’s room for improvement! I directly ported C code, but in C – strings end with \0. Maybe it’s possible to rewrite it a bit to be aware of string lengths? Also, there are no benchmarks currently.

1 Like

Cool :slight_smile: I sent you a PR to clean up a couple things.

2 Likes

I am reminded of a recent blog post I read on the same topic that might interest you: Human sorting improved | Ned Batchelder