[{"data":1,"prerenderedAt":935},["ShallowReactive",2],{"guide-natural-sort-order-explained":3},{"id":4,"title":5,"body":6,"date":924,"description":925,"extension":926,"meta":927,"navigation":326,"path":931,"readingTime":573,"seo":932,"stem":933,"__hash__":934},"guides\u002Fguides\u002Fnatural-sort-order-explained.md","Natural Sort Order Explained: Why 10 Comes After 2, Not Before",{"type":7,"value":8,"toc":907},"minimark",[9,14,42,52,55,59,77,124,127,130,134,137,188,191,196,204,222,225,229,247,260,276,292,296,300,307,346,350,353,726,730,741,778,782,800,812,835,845,849,871,875,883,887,903],[10,11,13],"h2",{"id":12},"what-is-natural-sort-order","What Is Natural Sort Order?",[15,16,17,18,22,23,26,27,30,31,34,35,38,39,41],"p",{},"Natural sort order (also called \"human sort\" or \"alphanumeric sort\") arranges items the way people expect, not the way computers default to. The classic example: alphabetical sorting places ",[19,20,21],"code",{},"file10.txt"," before ",[19,24,25],{},"file2.txt"," because it compares character by character — and ",[19,28,29],{},"1"," comes before ",[19,32,33],{},"2",". Natural sort recognizes that ",[19,36,37],{},"10"," and ",[19,40,33],{}," are numbers and orders them by value instead.",[43,44,49],"pre",{"className":45,"code":47,"language":48},[46],"language-text","Alphabetical:  file1.txt, file10.txt, file2.txt, file20.txt\nNatural:       file1.txt, file2.txt, file10.txt, file20.txt\n","text",[19,50,47],{"__ignoreMap":51},"",[15,53,54],{},"The intuition is simple: read the string the way a human would, treating consecutive digit characters as a single numeric value.",[10,56,58],{"id":57},"why-standard-sorting-fails","Why Standard Sorting Fails",[15,60,61,62,65,66,69,70,38,73,76],{},"Computer sorting works by comparing character codes. In ASCII and Unicode, ",[19,63,64],{},"0"," through ",[19,67,68],{},"9"," have codes 48 through 57. When a sort algorithm compares ",[19,71,72],{},"file10",[19,74,75],{},"file2",", it examines characters one at a time:",[78,79,80,90,97,104,111],"ol",{},[81,82,83,86,87,89],"li",{},[19,84,85],{},"f"," vs ",[19,88,85],{}," — equal",[81,91,92,86,95,89],{},[19,93,94],{},"i",[19,96,94],{},[81,98,99,86,102,89],{},[19,100,101],{},"l",[19,103,101],{},[81,105,106,86,109,89],{},[19,107,108],{},"e",[19,110,108],{},[81,112,113,86,115,117,118,120,121,123],{},[19,114,29],{},[19,116,33],{}," — ",[19,119,29],{}," is less, so ",[19,122,72],{}," wins",[15,125,126],{},"The algorithm never looks beyond that first digit. From the computer's perspective, it did exactly what was asked. The problem is that what was asked does not match what humans expect.",[15,128,129],{},"This matters anywhere users see sorted lists: file explorers, version numbers, chapter titles, product codes with embedded numbers.",[10,131,133],{"id":132},"how-natural-sort-works","How Natural Sort Works",[15,135,136],{},"A natural sort algorithm splits each string into alternating text and number tokens, then compares token by token:",[138,139,140,153],"table",{},[141,142,143],"thead",{},[144,145,146,150],"tr",{},[147,148,149],"th",{},"String",[147,151,152],{},"Tokens",[154,155,156,174],"tbody",{},[144,157,158,163],{},[159,160,161],"td",{},[19,162,21],{},[159,164,165,168,169,168,171],{},[19,166,167],{},"file"," ",[19,170,37],{},[19,172,173],{},".txt",[144,175,176,180],{},[159,177,178],{},[19,179,25],{},[159,181,182,168,184,168,186],{},[19,183,167],{},[19,185,33],{},[19,187,173],{},[15,189,190],{},"When two tokens are both text, compare alphabetically. When both are numbers, compare numerically. When types differ, text sorts before numbers (or vice versa, depending on convention).",[192,193,195],"h3",{"id":194},"token-by-token-comparison","Token-by-Token Comparison",[15,197,198,199,38,201,203],{},"Comparing ",[19,200,21],{},[19,202,25],{},":",[78,205,206,212],{},[81,207,208,86,210,89],{},[19,209,167],{},[19,211,167],{},[81,213,214,86,216,218,219,221],{},[19,215,37],{},[19,217,33],{}," — numeric comparison: 10 > 2, so ",[19,220,25],{}," comes first",[15,223,224],{},"That is the entire algorithm. The key insight is recognizing digit sequences and treating them as integers.",[10,226,228],{"id":227},"common-use-cases","Common Use Cases",[15,230,231,235,236,239,240,239,243,246],{},[232,233,234],"strong",{},"File names."," Versioned files like ",[19,237,238],{},"report_q1.pdf",", ",[19,241,242],{},"report_q2.pdf",[19,244,245],{},"report_q10.pdf"," look wrong under alphabetical sort. Natural sort puts them in the expected sequence.",[15,248,249,252,253,86,256,259],{},[232,250,251],{},"Version numbers."," While full semver parsing (like ",[19,254,255],{},"1.2.10",[19,257,258],{},"1.2.9",") requires additional logic, natural sort handles the simple case where numbers are embedded in strings.",[15,261,262,265,266,239,269,239,272,275],{},[232,263,264],{},"Numbered lists."," Tutorials, legal clauses, and outlines often use mixed numbering: ",[19,267,268],{},"Step 1",[19,270,271],{},"Step 2",[19,273,274],{},"Step 10",". Natural sort keeps them in order.",[15,277,278,281,282,239,285,239,288,291],{},[232,279,280],{},"Product SKUs."," Codes like ",[19,283,284],{},"PROD-100",[19,286,287],{},"PROD-200",[19,289,290],{},"PROD-1010"," sort correctly when digits are compared by value.",[10,293,295],{"id":294},"natural-sort-in-programming-languages","Natural Sort in Programming Languages",[192,297,299],{"id":298},"python","Python",[15,301,302,303,306],{},"Python 3 does not have a built-in natural sort, but the ",[19,304,305],{},"natsort"," library handles it:",[43,308,311],{"className":309,"code":310,"language":298,"meta":51,"style":51},"language-python shiki shiki-themes github-light github-dark","from natsort import natsorted\n\nfiles = ['file10.txt', 'file2.txt', 'file1.txt']\nprint(natsorted(files))\n# ['file1.txt', 'file2.txt', 'file10.txt']\n",[19,312,313,321,328,334,340],{"__ignoreMap":51},[314,315,318],"span",{"class":316,"line":317},"line",1,[314,319,320],{},"from natsort import natsorted\n",[314,322,324],{"class":316,"line":323},2,[314,325,327],{"emptyLinePlaceholder":326},true,"\n",[314,329,331],{"class":316,"line":330},3,[314,332,333],{},"files = ['file10.txt', 'file2.txt', 'file1.txt']\n",[314,335,337],{"class":316,"line":336},4,[314,338,339],{},"print(natsorted(files))\n",[314,341,343],{"class":316,"line":342},5,[314,344,345],{},"# ['file1.txt', 'file2.txt', 'file10.txt']\n",[192,347,349],{"id":348},"javascript","JavaScript",[15,351,352],{},"A simple implementation tokenizes and compares:",[43,354,357],{"className":355,"code":356,"language":348,"meta":51,"style":51},"language-javascript shiki shiki-themes github-light github-dark","function naturalSort(a, b) {\n  const ax = [], bx = [];\n  a.replace(\u002F(\\d+)|(\\D+)\u002Fg, (_, $1, $2) => { ax.push([$1 || Infinity, $2 || '']) });\n  b.replace(\u002F(\\d+)|(\\D+)\u002Fg, (_, $1, $2) => { bx.push([$1 || Infinity, $2 || '']) });\n\n  while (ax.length && bx.length) {\n    const an = ax.shift();\n    const bn = bx.shift();\n    const nn = (an[0] - bn[0]) || an[1].localeCompare(bn[1]);\n    if (nn) return nn;\n  }\n  return ax.length - bx.length;\n}\n",[19,358,359,385,408,499,567,571,593,613,629,679,694,700,720],{"__ignoreMap":51},[314,360,361,365,369,373,377,379,382],{"class":316,"line":317},[314,362,364],{"class":363},"szBVR","function",[314,366,368],{"class":367},"sScJk"," naturalSort",[314,370,372],{"class":371},"sVt8B","(",[314,374,376],{"class":375},"s4XuR","a",[314,378,239],{"class":371},[314,380,381],{"class":375},"b",[314,383,384],{"class":371},") {\n",[314,386,387,390,394,397,400,403,405],{"class":316,"line":323},[314,388,389],{"class":363},"  const",[314,391,393],{"class":392},"sj4cs"," ax",[314,395,396],{"class":363}," =",[314,398,399],{"class":371}," [], ",[314,401,402],{"class":392},"bx",[314,404,396],{"class":363},[314,406,407],{"class":371}," [];\n",[314,409,410,413,416,418,422,425,428,431,434,437,439,442,444,446,448,451,454,457,459,462,464,467,470,473,476,479,482,485,488,491,493,496],{"class":316,"line":330},[314,411,412],{"class":371},"  a.",[314,414,415],{"class":367},"replace",[314,417,372],{"class":371},[314,419,421],{"class":420},"sZZnC","\u002F",[314,423,372],{"class":424},"sA_wV",[314,426,427],{"class":392},"\\d",[314,429,430],{"class":363},"+",[314,432,433],{"class":424},")",[314,435,436],{"class":363},"|",[314,438,372],{"class":424},[314,440,441],{"class":392},"\\D",[314,443,430],{"class":363},[314,445,433],{"class":424},[314,447,421],{"class":420},[314,449,450],{"class":363},"g",[314,452,453],{"class":371},", (",[314,455,456],{"class":375},"_",[314,458,239],{"class":371},[314,460,461],{"class":375},"$1",[314,463,239],{"class":371},[314,465,466],{"class":375},"$2",[314,468,469],{"class":371},") ",[314,471,472],{"class":363},"=>",[314,474,475],{"class":371}," { ax.",[314,477,478],{"class":367},"push",[314,480,481],{"class":371},"([$1 ",[314,483,484],{"class":363},"||",[314,486,487],{"class":392}," Infinity",[314,489,490],{"class":371},", $2 ",[314,492,484],{"class":363},[314,494,495],{"class":420}," ''",[314,497,498],{"class":371},"]) });\n",[314,500,501,504,506,508,510,512,514,516,518,520,522,524,526,528,530,532,534,536,538,540,542,544,546,548,551,553,555,557,559,561,563,565],{"class":316,"line":336},[314,502,503],{"class":371},"  b.",[314,505,415],{"class":367},[314,507,372],{"class":371},[314,509,421],{"class":420},[314,511,372],{"class":424},[314,513,427],{"class":392},[314,515,430],{"class":363},[314,517,433],{"class":424},[314,519,436],{"class":363},[314,521,372],{"class":424},[314,523,441],{"class":392},[314,525,430],{"class":363},[314,527,433],{"class":424},[314,529,421],{"class":420},[314,531,450],{"class":363},[314,533,453],{"class":371},[314,535,456],{"class":375},[314,537,239],{"class":371},[314,539,461],{"class":375},[314,541,239],{"class":371},[314,543,466],{"class":375},[314,545,469],{"class":371},[314,547,472],{"class":363},[314,549,550],{"class":371}," { bx.",[314,552,478],{"class":367},[314,554,481],{"class":371},[314,556,484],{"class":363},[314,558,487],{"class":392},[314,560,490],{"class":371},[314,562,484],{"class":363},[314,564,495],{"class":420},[314,566,498],{"class":371},[314,568,569],{"class":316,"line":342},[314,570,327],{"emptyLinePlaceholder":326},[314,572,574,577,580,583,586,589,591],{"class":316,"line":573},6,[314,575,576],{"class":363},"  while",[314,578,579],{"class":371}," (ax.",[314,581,582],{"class":392},"length",[314,584,585],{"class":363}," &&",[314,587,588],{"class":371}," bx.",[314,590,582],{"class":392},[314,592,384],{"class":371},[314,594,596,599,602,604,607,610],{"class":316,"line":595},7,[314,597,598],{"class":363},"    const",[314,600,601],{"class":392}," an",[314,603,396],{"class":363},[314,605,606],{"class":371}," ax.",[314,608,609],{"class":367},"shift",[314,611,612],{"class":371},"();\n",[314,614,616,618,621,623,625,627],{"class":316,"line":615},8,[314,617,598],{"class":363},[314,619,620],{"class":392}," bn",[314,622,396],{"class":363},[314,624,588],{"class":371},[314,626,609],{"class":367},[314,628,612],{"class":371},[314,630,632,634,637,639,642,644,647,650,653,655,658,660,663,665,668,671,674,676],{"class":316,"line":631},9,[314,633,598],{"class":363},[314,635,636],{"class":392}," nn",[314,638,396],{"class":363},[314,640,641],{"class":371}," (an[",[314,643,64],{"class":392},[314,645,646],{"class":371},"] ",[314,648,649],{"class":363},"-",[314,651,652],{"class":371}," bn[",[314,654,64],{"class":392},[314,656,657],{"class":371},"]) ",[314,659,484],{"class":363},[314,661,662],{"class":371}," an[",[314,664,29],{"class":392},[314,666,667],{"class":371},"].",[314,669,670],{"class":367},"localeCompare",[314,672,673],{"class":371},"(bn[",[314,675,29],{"class":392},[314,677,678],{"class":371},"]);\n",[314,680,682,685,688,691],{"class":316,"line":681},10,[314,683,684],{"class":363},"    if",[314,686,687],{"class":371}," (nn) ",[314,689,690],{"class":363},"return",[314,692,693],{"class":371}," nn;\n",[314,695,697],{"class":316,"line":696},11,[314,698,699],{"class":371},"  }\n",[314,701,703,706,708,710,713,715,717],{"class":316,"line":702},12,[314,704,705],{"class":363},"  return",[314,707,606],{"class":371},[314,709,582],{"class":392},[314,711,712],{"class":363}," -",[314,714,588],{"class":371},[314,716,582],{"class":392},[314,718,719],{"class":371},";\n",[314,721,723],{"class":316,"line":722},13,[314,724,725],{"class":371},"}\n",[192,727,729],{"id":728},"linux","Linux",[15,731,732,733,736,737,740],{},"The ",[19,734,735],{},"sort"," command supports natural ordering with ",[19,738,739],{},"-V"," (version sort):",[43,742,746],{"className":743,"code":744,"language":745,"meta":51,"style":51},"language-bash shiki shiki-themes github-light github-dark","ls | sort -V\n# file1.txt\n# file2.txt\n# file10.txt\n","bash",[19,747,748,762,768,773],{"__ignoreMap":51},[314,749,750,753,756,759],{"class":316,"line":317},[314,751,752],{"class":367},"ls",[314,754,755],{"class":363}," |",[314,757,758],{"class":367}," sort",[314,760,761],{"class":392}," -V\n",[314,763,764],{"class":316,"line":323},[314,765,767],{"class":766},"sJ8bj","# file1.txt\n",[314,769,770],{"class":316,"line":330},[314,771,772],{"class":766},"# file2.txt\n",[314,774,775],{"class":316,"line":336},[314,776,777],{"class":766},"# file10.txt\n",[10,779,781],{"id":780},"edge-cases-to-watch","Edge Cases to Watch",[15,783,784,168,787,86,790,793,794,38,797,799],{},[232,785,786],{},"Leading zeros.",[19,788,789],{},"file001.txt",[19,791,792],{},"file1.txt"," — should leading zeros be treated as padding or as significant digits? Most natural sort implementations treat ",[19,795,796],{},"001",[19,798,29],{}," as equivalent, but some preserve leading zeros as a tiebreaker.",[15,801,802,168,805,86,808,811],{},[232,803,804],{},"Negative numbers.",[19,806,807],{},"temp-5.txt",[19,809,810],{},"temp10.txt"," — deciding whether the hyphen is part of the number or a separator changes the result.",[15,813,814,168,817,86,820,823,824,827,828,239,830,239,833,832],{},[232,815,816],{},"Decimal numbers.",[19,818,819],{},"v1.10",[19,821,822],{},"v1.2"," — should the dot be a decimal point or a token separator? Most natural sort implementations treat the dot as a separator, splitting ",[19,825,826],{},"1.10"," into tokens ",[19,829,29],{},[19,831,832],{},".",[19,834,37],{},[15,836,837,840,841,844],{},[232,838,839],{},"Unicode digits."," Full-width digits like ",[19,842,843],{},"２"," (U+FF12) may not be recognized by simple regex-based implementations.",[10,846,848],{"id":847},"key-takeaways","Key Takeaways",[850,851,852,855,862,865,868],"ul",{},[81,853,854],{},"Natural sort compares digit sequences numerically while comparing text alphabetically — matching human expectations",[81,856,857,858,30,860],{},"Standard alphabetical sort treats digits as characters, so ",[19,859,37],{},[19,861,33],{},[81,863,864],{},"The algorithm splits strings into text and number tokens, then compares token by token",[81,866,867],{},"Leading zeros, negative signs, and decimal points are edge cases where implementations differ",[81,869,870],{},"Most programming languages require a library or custom function for natural sort",[10,872,874],{"id":873},"try-it-yourself","Try It Yourself",[15,876,877,878,882],{},"Sort any list with natural order using our free ",[376,879,881],{"href":880},"\u002Ftools\u002Flist-sorter","List Sorter",". Paste your items, select the sorting mode, and get instantly organized results — no code needed.",[10,884,886],{"id":885},"related-guides","Related Guides",[850,888,889,896],{},[81,890,891,895],{},[376,892,894],{"href":893},"\u002Fguides\u002Flist-sorting-guide","List Sorting Guide"," — overview of all sorting modes and when to use each",[81,897,898,902],{},[376,899,901],{"href":900},"\u002Fguides\u002Falphabetical-sort-algorithms","Alphabetical Sort Algorithms"," — how standard sorting works under the hood",[904,905,906],"style",{},"html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .szBVR, html code.shiki .szBVR{--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sScJk, html code.shiki .sScJk{--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sVt8B, html code.shiki .sVt8B{--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .s4XuR, html code.shiki .s4XuR{--shiki-default:#E36209;--shiki-dark:#FFAB70}html pre.shiki code .sj4cs, html code.shiki .sj4cs{--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sZZnC, html code.shiki .sZZnC{--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .sA_wV, html code.shiki .sA_wV{--shiki-default:#032F62;--shiki-dark:#DBEDFF}html pre.shiki code .sJ8bj, html code.shiki .sJ8bj{--shiki-default:#6A737D;--shiki-dark:#6A737D}",{"title":51,"searchDepth":323,"depth":323,"links":908},[909,910,911,914,915,920,921,922,923],{"id":12,"depth":323,"text":13},{"id":57,"depth":323,"text":58},{"id":132,"depth":323,"text":133,"children":912},[913],{"id":194,"depth":330,"text":195},{"id":227,"depth":323,"text":228},{"id":294,"depth":323,"text":295,"children":916},[917,918,919],{"id":298,"depth":330,"text":299},{"id":348,"depth":330,"text":349},{"id":728,"depth":330,"text":729},{"id":780,"depth":323,"text":781},{"id":847,"depth":323,"text":848},{"id":873,"depth":323,"text":874},{"id":885,"depth":323,"text":886},"2026-05-28","Understand natural sort order and how it differs from alphabetical sorting. Learn why file10.txt appears after file9.txt with natural sorting.","md",{"keywords":928,"immutable":326},[929,930],"list-sorter","natural-sort-order-explained","\u002Fguides\u002Fnatural-sort-order-explained",{"title":5,"description":925},"guides\u002Fnatural-sort-order-explained","r5UdeHk5zsWKy5FF7lGME_ZPm8ERehHOXrLQZuYotn4",1780401335391]