Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've noticed than many numerical sorting methods seem to sort by 1, 10, 2, 3... rather than the expected 1, 2, 3, 10... I'm having trouble coming up with a scenario where I would need the first method and, as a user, I get frustrated whenever I see it in practice. Are there legitimate use cases for the first style over the second? If so, what are they? If not, how did the first sort style ever come into being? What are the official names for each sort method?

share|improve this question
Not an answer to your question, but if you have to sort a list of strings which could contain numbers, you probably want to use the Alphanum algorithm: – TehShrike Jan 1 '12 at 11:28
up vote 37 down vote accepted

that is lexicographic sorting which means basically the language treats the variables as strings and compares character by character ("200" is greater than "19999" because '2' is greater than '1')

to fix this you can

  • ensure that the values are treated as integers,

  • prepend '0' to the strings so all have equal lengths (only viable when you know the max value).
    This is why you'll see episode numberings on media files (S1E01) with a prepended 0 so a lexicographic sort doesn't mess things up and allows programs to simply play/display in alphabetical order,

  • or make a custom comparator that first compares the length of the strings (shorter strings being smaller integers) and when they are equal compare the lexicographically (careful about leading '0')

share|improve this answer
+1 for 'lexiographic'. Never heard that term, I would just have thought of this as alphabetic sorting - the numbers are being treated as a string type, like you said. – Anonymous Dec 30 '11 at 9:30

Alphabetically, 1 comes before 2. Whenever you see the first method, it's not because it's desirable, but because the sorting is strictly alphabetical (and happens left-to-right, one character at a time): 1, 2, 10 makes sense to you but not to a computer that only knows alphabetic comparison. There's no way in that kind of simple comparison to know that a one followed by a 0 actually comes after a two.

When you see mixed word and number sorting that treats numbers correctly, it's because the sorting is more intelligent, and on top of that, still usually only works at the beginning or end of a string.

share|improve this answer

That's the result when you sort strings of numbers alphabetically instead of numerically.

That sort style is the default behavior of the unix sort command for example, unless you use the --numeric-sort command line option, which tells it to attempt to interpret the numeric values.

share|improve this answer

Others have answers what this sort is, but no one every really answered your question about why you see it. The answer isn't really that exciting. It's usually a bug. Most sorting methods will default to one or the other and the programming likely careless of changing the default when sorting numbers.

share|improve this answer

protected by gnat Aug 10 '15 at 15:37

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.