14,000 hints and counting!

The following comments are owned by whoever posted them. This site is not responsible for what they say.
Authored by: Mikey-San on Jul 25, '03 12:00:04PM

Right after I sent in this hint, it hit me:

It's a coordinate system, as the manual states (hey, I'm not perfect when it comes to reading man pages ;-))

If I have these folders:

test3folder
test40folder
test5folder

I can sort them by using +0.4. Why? The first number, 0, tells sort to search the first field (0 is first field, 1 is second, 2 is third, and so on), and 4 tells sort to sort starting with the fifth character (0 is first, 1 is second, and so on like the first coordinate number).

:-)

Authored by: Clewin on Jul 25, '03 01:32:44PM

I've always disliked the terminology of the sort man page, which is pretty much written for advanced programmers - a simpler explanation is that the first value is the word you want to sort on, and the second value is the character in that word. It's actually a bit more complex than that, as it is based on string tokenization (strings are broken up by a character specified by the -t parameter, default is space), but the concept is easier to understand this way.

Remember that the first word is 0, not 1 (this is due to really old legacy issues that also crept into the C language, btw) - here's a table by words:
0 1 2
----------------------------------
untitled folder 1
untitled folder 2
untitled folder 22
untitled folder 3

sort +2 is the same as saying sort +2.0. The 2 is the third word/column above, and the 0 refers to the first character in that column (in order, 1,2,2,3). This obviously won't work as is, as you will get the same order as above. This is where the -n flag comes in - it translates the value to a number first, and then performs the sort. Since 22 comes after 3, it is last in the list when sorted as a number.

Incidentally, there is an alternate way of writing this command, as well.
ls | sort +2n

Alas, not all your problems are solved - if you have something like this:
untitled 1
untitled 22
untitled 3
untitled folder 1
untitled folder 11
untitled folder 2

how do you sort that?

sort has the ability to handle multiple inputs, so you could do something like
ls | sort -d +2n +1n
note the reverse order number sort - if you don't do that, words will take precedence over number (actually, they're viewed as no number) and will appear first - therefore, untitled folder 1 will show up before untitled 1.

'apple folder 1'
will break it again. The solution isn't easy, and I'm pretty sure can't be solved with sort alone, at least not in a case that works for everything. How I would probably handle it:
write a program that does a sort +0
pass matching '0' tokens (e.g. any untitled above) into a second level sort that sorts on +1. Recurse until all tokens are parsed. On the reverse recursion, sort +[level]n so numbers get sorted properly.
alias ls to this program.