[{"data":1,"prerenderedAt":682},["ShallowReactive",2],{"guide-sorting-mixed-data-types":3},{"id":4,"title":5,"body":6,"date":670,"description":671,"extension":672,"meta":673,"navigation":677,"path":678,"readingTime":530,"seo":679,"stem":680,"__hash__":681},"guides\u002Fguides\u002Fsorting-mixed-data-types.md","Sorting Mixed Data Types: Numbers, Text, and Dates in One List",{"type":7,"value":8,"toc":651},"minimark",[9,14,18,30,33,37,40,99,107,111,114,136,156,161,171,174,178,185,196,202,206,209,213,216,238,241,245,306,310,314,340,359,363,588,592,615,619,627,631,647],[10,11,13],"h2",{"id":12},"the-mixed-type-sorting-problem","The Mixed-Type Sorting Problem",[15,16,17],"p",{},"Real-world data is rarely uniform. A spreadsheet column might contain numbers, text labels, dates, and blank cells all mixed together. When you sort this kind of data, the results depend entirely on how the sorting tool handles type differences.",[15,19,20,21,25,26,29],{},"Sort a list like ",[22,23,24],"code",{},"100, apple, 42, banana, 7"," alphabetically and you get ",[22,27,28],{},"100, 42, 7, apple, banana"," — because the comparison treats everything as text. Sort it numerically and the text entries either get pushed to one end or cause errors.",[15,31,32],{},"Understanding how mixed-type sorting works helps you predict results and choose the right approach.",[10,34,36],{"id":35},"type-comparison-rules","Type Comparison Rules",[15,38,39],{},"When a sorting algorithm compares two values of different types, it needs a rule. There are three common strategies:",[41,42,43,59],"table",{},[44,45,46],"thead",{},[47,48,49,53,56],"tr",{},[50,51,52],"th",{},"Strategy",[50,54,55],{},"How It Works",[50,57,58],{},"Example Order",[60,61,62,75,88],"tbody",{},[47,63,64,68,71],{},[65,66,67],"td",{},"All-as-text",[65,69,70],{},"Convert everything to strings first",[65,72,73],{},[22,74,28],{},[47,76,77,80,83],{},[65,78,79],{},"Type priority",[65,81,82],{},"Sort by type group, then within type",[65,84,85],{},[22,86,87],{},"7, 42, 100, apple, banana",[47,89,90,93,96],{},[65,91,92],{},"Error on mismatch",[65,94,95],{},"Reject lists with mixed types",[65,97,98],{},"Throws error",[15,100,101,102,106],{},"Most practical tools use ",[103,104,105],"strong",{},"type priority",": numbers sort together first (by value), then text (alphabetically), then dates (chronologically). This produces the most intuitive result.",[10,108,110],{"id":109},"numbers-vs-text-strings","Numbers vs. Text Strings",[15,112,113],{},"The most common mixed-type scenario is numbers and text in the same list.",[15,115,116,119,120,123,124,127,128,131,132,135],{},[103,117,118],{},"Alphabetical sort"," compares character codes, so ",[22,121,122],{},"100"," comes before ",[22,125,126],{},"42"," because ",[22,129,130],{},"1"," \u003C ",[22,133,134],{},"4",". This is technically correct but visually wrong.",[15,137,138,141,142,144,145,147,148,151,152,155],{},[103,139,140],{},"Numeric sort"," treats ",[22,143,122],{}," and ",[22,146,126],{}," as numbers and orders them by value: ",[22,149,150],{},"42, 100",". Text entries like ",[22,153,154],{},"apple"," cannot be parsed as numbers, so they fall to the end or get grouped separately.",[157,158,160],"h3",{"id":159},"practical-example","Practical Example",[162,163,168],"pre",{"className":164,"code":166,"language":167},[165],"language-text","Input:    100, apple, 42, banana, 7, cherry\n\nType priority sort (numbers first):\n         7, 42, 100, apple, banana, cherry\n\nType priority sort (text first):\n         apple, banana, cherry, 7, 42, 100\n","text",[22,169,166],{"__ignoreMap":170},"",[15,172,173],{},"The choice of which group comes first depends on convention. Numeric-first is more common because data lists tend to have numerical entries at the top.",[10,175,177],{"id":176},"sorting-dates-alongside-other-types","Sorting Dates Alongside Other Types",[15,179,180,181,184],{},"Dates add another layer. The string ",[22,182,183],{},"2026-01-15"," looks like text to a naive sort, but semantically it represents a point in time.",[15,186,187,188,191,192,195],{},"If your tool recognizes date formats (ISO 8601, MM\u002FDD\u002FYYYY, etc.), it can parse them into timestamps and sort chronologically. If not, dates sort as text — which happens to work correctly for ISO format (",[22,189,190],{},"YYYY-MM-DD",") but fails for formats like ",[22,193,194],{},"01\u002F15\u002F2026",".",[162,197,200],{"className":198,"code":199,"language":167},[165],"ISO dates sorted as text (correct):\n  2025-03-01, 2025-12-01, 2026-01-15\n\nUS dates sorted as text (wrong):\n  01\u002F15\u002F2026, 03\u002F01\u002F2025, 12\u002F01\u002F2025\n",[22,201,199],{"__ignoreMap":170},[157,203,205],{"id":204},"recommendation","Recommendation",[15,207,208],{},"Convert dates to ISO 8601 format before sorting if your tool lacks date-aware parsing. This guarantees correct chronological order even with text-based sorting.",[10,210,212],{"id":211},"handling-blank-lines-and-empty-values","Handling Blank Lines and Empty Values",[15,214,215],{},"Blank lines and empty cells are a special type. They carry no information but disrupt sorting:",[217,218,219,226,232],"ul",{},[220,221,222,225],"li",{},[103,223,224],{},"Sorted to the top"," — blanks appear first in ascending order (empty string sorts before any character)",[220,227,228,231],{},[103,229,230],{},"Sorted to the bottom"," — some tools push blanks to the end regardless of direction",[220,233,234,237],{},[103,235,236],{},"Removed"," — the cleanest option when blanks serve no purpose",[15,239,240],{},"Most list sorters include a \"remove blank lines\" option. Use it unless blanks are intentional separators.",[10,242,244],{"id":243},"sorting-strategy-by-data-composition","Sorting Strategy by Data Composition",[41,246,247,257],{},[44,248,249],{},[47,250,251,254],{},[50,252,253],{},"Data Composition",[50,255,256],{},"Recommended Approach",[60,258,259,266,274,282,290,298],{},[47,260,261,264],{},[65,262,263],{},"All numbers",[65,265,140],{},[47,267,268,271],{},[65,269,270],{},"All text",[65,272,273],{},"Alphabetical sort (case-insensitive)",[47,275,276,279],{},[65,277,278],{},"Numbers + text",[65,280,281],{},"Type priority sort (numbers first)",[47,283,284,287],{},[65,285,286],{},"Numbers + dates",[65,288,289],{},"Numeric sort after converting dates to timestamps",[47,291,292,295],{},[65,293,294],{},"Mixed numbers, text, dates",[65,296,297],{},"Type priority with date parsing",[47,299,300,303],{},[65,301,302],{},"Versions (1.2.10)",[65,304,305],{},"Natural sort or version sort",[10,307,309],{"id":308},"programming-language-approaches","Programming Language Approaches",[157,311,313],{"id":312},"python","Python",[162,315,318],{"className":316,"code":317,"language":312,"meta":170,"style":170},"language-python shiki shiki-themes github-light github-dark","mixed = ['100', 'apple', '42', 'banana', '7']\nsorted_list = sorted(mixed, key=lambda x: (not x.isdigit(), int(x) if x.isdigit() else x.lower()))\n# Result: ['7', '42', '100', 'apple', 'banana']\n",[22,319,320,328,334],{"__ignoreMap":170},[321,322,325],"span",{"class":323,"line":324},"line",1,[321,326,327],{},"mixed = ['100', 'apple', '42', 'banana', '7']\n",[321,329,331],{"class":323,"line":330},2,[321,332,333],{},"sorted_list = sorted(mixed, key=lambda x: (not x.isdigit(), int(x) if x.isdigit() else x.lower()))\n",[321,335,337],{"class":323,"line":336},3,[321,338,339],{},"# Result: ['7', '42', '100', 'apple', 'banana']\n",[15,341,342,343,346,347,350,351,354,355,358],{},"The key function assigns a tuple: ",[22,344,345],{},"(False, numeric_value)"," for numbers and ",[22,348,349],{},"(True, lowercase_text)"," for strings. Python sorts tuples element-by-element, so all numbers (with ",[22,352,353],{},"False",") come before all text (with ",[22,356,357],{},"True",").",[157,360,362],{"id":361},"javascript","JavaScript",[162,364,367],{"className":365,"code":366,"language":361,"meta":170,"style":170},"language-javascript shiki shiki-themes github-light github-dark","const mixed = ['100', 'apple', '42', 'banana', '7'];\nmixed.sort((a, b) => {\n  const aNum = Number(a), bNum = Number(b);\n  const aIsNum = !isNaN(aNum), bIsNum = !isNaN(bNum);\n  if (aIsNum && bIsNum) return aNum - bNum;\n  if (aIsNum) return -1;\n  if (bIsNum) return 1;\n  return a.localeCompare(b);\n});\n\u002F\u002F Result: ['7', '42', '100', 'apple', 'banana']\n",[22,368,369,414,444,470,501,528,546,561,575,581],{"__ignoreMap":170},[321,370,371,375,379,382,386,390,393,396,398,401,403,406,408,411],{"class":323,"line":324},[321,372,374],{"class":373},"szBVR","const",[321,376,378],{"class":377},"sj4cs"," mixed",[321,380,381],{"class":373}," =",[321,383,385],{"class":384},"sVt8B"," [",[321,387,389],{"class":388},"sZZnC","'100'",[321,391,392],{"class":384},", ",[321,394,395],{"class":388},"'apple'",[321,397,392],{"class":384},[321,399,400],{"class":388},"'42'",[321,402,392],{"class":384},[321,404,405],{"class":388},"'banana'",[321,407,392],{"class":384},[321,409,410],{"class":388},"'7'",[321,412,413],{"class":384},"];\n",[321,415,416,419,423,426,430,432,435,438,441],{"class":323,"line":330},[321,417,418],{"class":384},"mixed.",[321,420,422],{"class":421},"sScJk","sort",[321,424,425],{"class":384},"((",[321,427,429],{"class":428},"s4XuR","a",[321,431,392],{"class":384},[321,433,434],{"class":428},"b",[321,436,437],{"class":384},") ",[321,439,440],{"class":373},"=>",[321,442,443],{"class":384}," {\n",[321,445,446,449,452,454,457,460,463,465,467],{"class":323,"line":336},[321,447,448],{"class":373},"  const",[321,450,451],{"class":377}," aNum",[321,453,381],{"class":373},[321,455,456],{"class":421}," Number",[321,458,459],{"class":384},"(a), ",[321,461,462],{"class":377},"bNum",[321,464,381],{"class":373},[321,466,456],{"class":421},[321,468,469],{"class":384},"(b);\n",[321,471,473,475,478,480,483,486,489,492,494,496,498],{"class":323,"line":472},4,[321,474,448],{"class":373},[321,476,477],{"class":377}," aIsNum",[321,479,381],{"class":373},[321,481,482],{"class":373}," !",[321,484,485],{"class":421},"isNaN",[321,487,488],{"class":384},"(aNum), ",[321,490,491],{"class":377},"bIsNum",[321,493,381],{"class":373},[321,495,482],{"class":373},[321,497,485],{"class":421},[321,499,500],{"class":384},"(bNum);\n",[321,502,504,507,510,513,516,519,522,525],{"class":323,"line":503},5,[321,505,506],{"class":373},"  if",[321,508,509],{"class":384}," (aIsNum ",[321,511,512],{"class":373},"&&",[321,514,515],{"class":384}," bIsNum) ",[321,517,518],{"class":373},"return",[321,520,521],{"class":384}," aNum ",[321,523,524],{"class":373},"-",[321,526,527],{"class":384}," bNum;\n",[321,529,531,533,536,538,541,543],{"class":323,"line":530},6,[321,532,506],{"class":373},[321,534,535],{"class":384}," (aIsNum) ",[321,537,518],{"class":373},[321,539,540],{"class":373}," -",[321,542,130],{"class":377},[321,544,545],{"class":384},";\n",[321,547,549,551,554,556,559],{"class":323,"line":548},7,[321,550,506],{"class":373},[321,552,553],{"class":384}," (bIsNum) ",[321,555,518],{"class":373},[321,557,558],{"class":377}," 1",[321,560,545],{"class":384},[321,562,564,567,570,573],{"class":323,"line":563},8,[321,565,566],{"class":373},"  return",[321,568,569],{"class":384}," a.",[321,571,572],{"class":421},"localeCompare",[321,574,469],{"class":384},[321,576,578],{"class":323,"line":577},9,[321,579,580],{"class":384},"});\n",[321,582,584],{"class":323,"line":583},10,[321,585,587],{"class":586},"sJ8bj","\u002F\u002F Result: ['7', '42', '100', 'apple', 'banana']\n",[10,589,591],{"id":590},"key-takeaways","Key Takeaways",[217,593,594,597,606,609,612],{},[220,595,596],{},"Mixed-type sorting requires a type priority rule — numbers first, then text, then dates — to produce intuitive results",[220,598,599,600,602,603,605],{},"Alphabetical sort on mixed data treats numbers as text, producing misleading order (",[22,601,122],{}," before ",[22,604,126],{},")",[220,607,608],{},"Dates should be in ISO 8601 format if your tool cannot parse date strings",[220,610,611],{},"Blank lines should typically be removed before sorting to avoid cluttering the result",[220,613,614],{},"In code, a custom key or comparator that checks types gives you full control over the sort order",[10,616,618],{"id":617},"try-it-yourself","Try It Yourself",[15,620,621,622,626],{},"Paste any mixed list into our ",[429,623,625],{"href":624},"\u002Ftools\u002Flist-sorter","List Sorter"," and instantly see it organized. Toggle between alphabetical and numeric modes to compare results side by side.",[10,628,630],{"id":629},"related-guides","Related Guides",[217,632,633,640],{},[220,634,635,639],{},[429,636,638],{"href":637},"\u002Fguides\u002Flist-sorting-guide","List Sorting Guide"," — all sorting modes and practical workflows",[220,641,642,646],{},[429,643,645],{"href":644},"\u002Fguides\u002Fdata-cleanup-tools","Data Cleanup Tools"," — removing blanks, duplicates, and whitespace",[648,649,650],"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 .sj4cs, html code.shiki .sj4cs{--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sVt8B, html code.shiki .sVt8B{--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sZZnC, html code.shiki .sZZnC{--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .sScJk, html code.shiki .sScJk{--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .s4XuR, html code.shiki .s4XuR{--shiki-default:#E36209;--shiki-dark:#FFAB70}html pre.shiki code .sJ8bj, html code.shiki .sJ8bj{--shiki-default:#6A737D;--shiki-dark:#6A737D}",{"title":170,"searchDepth":330,"depth":330,"links":652},[653,654,655,658,661,662,663,667,668,669],{"id":12,"depth":330,"text":13},{"id":35,"depth":330,"text":36},{"id":109,"depth":330,"text":110,"children":656},[657],{"id":159,"depth":336,"text":160},{"id":176,"depth":330,"text":177,"children":659},[660],{"id":204,"depth":336,"text":205},{"id":211,"depth":330,"text":212},{"id":243,"depth":330,"text":244},{"id":308,"depth":330,"text":309,"children":664},[665,666],{"id":312,"depth":336,"text":313},{"id":361,"depth":336,"text":362},{"id":590,"depth":330,"text":591},{"id":617,"depth":330,"text":618},{"id":629,"depth":330,"text":630},"2026-05-28","Learn how to sort lists containing mixed data types — numbers, text strings, and dates — without losing information or producing confusing results.","md",{"keywords":674,"immutable":677},[675,676],"list-sorter","sorting-mixed-data-types",true,"\u002Fguides\u002Fsorting-mixed-data-types",{"title":5,"description":671},"guides\u002Fsorting-mixed-data-types","H3XRaZstGOKQoGRiKpoxWD_aZ03yK5HpYR34Pz-BHSY",1780401337017]