Home > Main > “Natural” Sorting

“Natural” Sorting

December 13th, 2007 Leave a comment Go to comments

Life could be so easy, according to CodingHorror.

Perhaps a more human-friendly natural sort option should be built into mainstream programming languages, too.

Just like that. So simple. Windows Vista Explorer does it right. Or does it? Maybe, if you are only considering English language and are only concerned about numbers. In any other case, we are talking about full-blown AI here. What about “The Beatles”? Should it sort under “B” or under “T”? How about Chinese language? How about lexicographical order in telephone books? What about diacritics in other languages? What about ordering “5 feet” and “2 meters”?

I could go on forever. “Natural” sorting is only about perception. It’s personal. It’s different for every person. It’s context-sensitive. Actually, the word “natural” is a problem too: something natural for a German is not natural for a Japanese. It’s not something we can decide either. Do we really want our computers or, even worse, our programming language to decide for us? Don’t think so.

If you ask me for a solution: maybe we should stick to putting context next to our data. If we know it’s a date, sort it like a date. If it’s a file name, sort it “naturally”, i.e. in the context of a file system. And if you really need to, implement a “natural”, context- and number-sensitive algorithm just for this file names list. Don’t bother including it into every single language in the world, since everyone needs something else, you’ll risk turning your language into PHP. If you really want to, make a class and implement sorting on it. Or maybe write a lot of mixins for different kinds of numbers-aware sorting — bad luck if your favourite language doesn’t support mixins.

There are a lot of alternatives. I agree that ASCII-collating is the easiest one, which should be avoided. But keep in mind that there is no such thing as a “natural sorting order”.

Update: Yes, Jeff and also most of the commenters on his article are mainly talking about sorting file names. Like I stated above, sensible sorting is only possible within a context and with file names there is no context since we don’t know anything about the naming principle. File names is a principle that should be abandoned in the near future, we should be using meta-data instead. File sorting problem would become non-existent then.

Tags:
  1. No comments yet.
  1. No trackbacks yet.