

    – TehShrike
    Jan 1, 2012 at 11:28
  • It's very very simple. When sorting, the algorithm scans from left to right. So, when it comes to a 1 and a 5, the 5 is larger, and it just dumbly goes with this EVEN if the 1 is actually part of a larger number like 134234. To know that 134234 is larger than 5 we must actually scan past the number to the last digit(actually the first digit) 4 then work backwards and see that the one is actually a 100000 which is much larger than 5. So, your typical blind sort doesn't do this as it just compares character to character ignoring what occurs after(or before) in the comparison. Jun 30, 2017 at 17:07
  • 1
    If you read en.wikipedia.org/wiki/Natural_sort_order it should make sense. In natural order, strings of digits are grouped as a single "character". Not physically, just logically so we can still sort of have character comparisons like the first case, but we will be able to compare integer strings to integer strings rather than characters to characters, which will allow us to compare the full value. All sorts should be this way because this is the way we humans read things(for numbers, we actually read right to left, even in a left to right string 1234 = 1000+200+30+4, not 4000+300+20+1 Jun 30, 2017 at 17:11

4 Answers 4



to fix this you can

  • ensure that the values are treated as integers,



  • 8
    – Anonymous
    Dec 30, 2011 at 9:30
  • 4
    +1 for prepend '0' to the strings. I wasn't programming this, it was in the name of my folders and 'Chapter 10' was coming before 'Chapter 2'. Then I made chapters 1-9 called 01-09 and it is sorted "correctly" now.
    – Marvin
    Jan 1, 2019 at 12:47
  • 1
    Zero padding will "fix" the sort, but for anything other than to work around visualization problems this is sure to cause more work. I.e. you define a finite space, anyone/any system dealing with your data will have to implement the same zero padding with the "local knowledge" of how much padding...etc.
    – bryn
    Aug 31, 2020 at 10:05



  • //1, 2, 10 makes sense to you but not to a computer that only knows alphabetic comparison// in many programming languages, this is handled by data type. For example, 1,2,10 also makes sense to a computer if the values are all of type integer. The part that does not "make sense" is non-standardized treatment of strings that do not unambiguously map to type integer (e.g., abc123xyz). In some contexts, standards do actually exist for this type of issue, though, such as ISO 8601
    – dreftymac
    May 29, 2021 at 10:43



  • Also, newer versions of sort have a -V option that automatically handles complex situations like when lines contain multiple fields and IP addresses.
    – mchid
    Sep 1, 2020 at 23:14


  • 1
    In mixed alphabetic/numeric contexts, experienced users will tend to prefer lexiographic sorting, because it's consistent and predictable. Every app that tries to "intelligently" mix lexiographic and numeric sorting does so a little bit differently, making the sort of questionable utility.
    – j__m
    Jan 3, 2017 at 4:08
