Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Click here to return to the 'Addendum to hint' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Addendum to hint
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.

unfortunately, adding
'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.

[ Reply to This | # ]
Addendum to hint
Authored by: Mikey-San on Jul 25, '03 02:26:59PM

Yeah, the 0 for "first" came as no surprise, but not because I know C or anything . . .

Values in Forge (level editor for Marathon 2/Infinity) begin at 0 and go up from there, usually, as do values in Anvil (physics editor).


[ Reply to This | # ]