KKitForma.

Language

EnglishEnglishTürkçeTurkishDeutschGermanGuide unavailable in this language · open toolsEspañolSpanishGuide unavailable in this language · open toolsFrançaisFrenchGuide unavailable in this language · open toolsPortuguêsPortugueseGuide unavailable in this language · open toolsItalianoItalianGuide unavailable in this language · open toolsNederlandsDutchGuide unavailable in this language · open toolsPolskiPolishGuide unavailable in this language · open toolsРусскийRussianGuide unavailable in this language · open toolsУкраїнськаUkrainianGuide unavailable in this language · open toolsSvenskaSwedishGuide unavailable in this language · open toolsNorskNorwegianGuide unavailable in this language · open toolsDanskDanishGuide unavailable in this language · open toolsSuomiFinnishGuide unavailable in this language · open toolsČeštinaCzechGuide unavailable in this language · open toolsRomânăRomanianGuide unavailable in this language · open toolsΕλληνικάGreekGuide unavailable in this language · open toolsالعربيةArabicGuide unavailable in this language · open toolsעבריתHebrewGuide unavailable in this language · open toolsفارسیPersianGuide unavailable in this language · open toolsاردوUrduGuide unavailable in this language · open toolsहिन्दीHindiGuide unavailable in this language · open toolsবাংলাBengaliGuide unavailable in this language · open toolsதமிழ்TamilGuide unavailable in this language · open toolsతెలుగుTeluguGuide unavailable in this language · open toolsमराठीMarathiGuide unavailable in this language · open toolsગુજરાતીGujaratiGuide unavailable in this language · open tools简体中文Chinese SimplifiedGuide unavailable in this language · open tools繁體中文Chinese TraditionalGuide unavailable in this language · open tools日本語JapaneseGuide unavailable in this language · open tools한국어KoreanGuide unavailable in this language · open toolsTiếng ViệtVietnameseGuide unavailable in this language · open toolsไทยThaiGuide unavailable in this language · open toolsBahasa IndonesiaIndonesianGuide unavailable in this language · open toolsBahasa MelayuMalayGuide unavailable in this language · open toolsFilipinoFilipinoGuide unavailable in this language · open toolsKiswahiliSwahiliGuide unavailable in this language · open toolsAfrikaansAfrikaansGuide unavailable in this language · open toolsMagyarHungarianGuide unavailable in this language · open toolsБългарскиBulgarianGuide unavailable in this language · open toolsHrvatskiCroatianGuide unavailable in this language · open toolsSrpskiSerbianGuide unavailable in this language · open toolsSlovenčinaSlovakGuide unavailable in this language · open toolsSlovenščinaSlovenianGuide unavailable in this language · open toolsLietuviųLithuanianGuide unavailable in this language · open toolsLatviešuLatvianGuide unavailable in this language · open toolsEestiEstonianGuide unavailable in this language · open toolsCatalàCatalanGuide unavailable in this language · open toolsEuskaraBasqueGuide unavailable in this language · open tools
← Questions & answers

KITFORMA · EDITORIAL ANSWER

Why does a numeric CSV column sort 12 before 3 in JavaScript?

KitForma editorial guide. Sort parsed quantities numerically while keeping blank or malformed cells at the end and preserving identifiers as strings.

JavaScript & TypeScript →

Step-by-step answer

JavaScript’s default sort compares string representations, so "12" precedes "3". Decide which cells are valid quantities before sorting. This example accepts finite decimal quantities, keeps invalid cells last, and leaves the original array untouched. Runnable example:
javascript
const cells = ["12", "3", "", "3x", "0"];
function quantity(cell) {
  const value = cell.trim();
  if (!/^-?(?:\d+(?:\.\d*)?|\.\d+)$/.test(value)) return null;
  const n = Number(value);
  return Number.isFinite(n) ? n : null;
}
const sorted = cells.map(raw => ({ raw, n: quantity(raw) }))
  .sort((a, b) => a.n === null ? (b.n === null ? 0 : 1)
    : b.n === null ? -1 : a.n - b.n);
console.log(JSON.stringify(sorted.map(row => row.raw)));
console.log(JSON.stringify(cells));
Expected result:
text
["0","3","12","","3x"]
["12","3","","3x","0"]
Why it works: An empty string becomes zero with Number(""); parseFloat("3x") becomes 3. Both hide bad input here. Keeping the raw cell beside the parsed number makes rejected values reviewable. Equal invalid cells retain their input order in modern stable sort implementations. Scope and checks: Run in Node.js 24 or a modern browser console. This decimal policy deliberately excludes thousands separators, decimal commas and scientific notation. Parse CSV with a CSV parser first; never split quoted rows on commas. SKU 00125 is an identifier, not a quantity. Related tools and guides: Inspect a CSV sort with a small sample: https://kitforma.com/en/csv-sorter/ Windows CSV newline example: https://kitforma.com/en/community/questions/7aa10000-0000-4000-8000-000000007340/ Primary sources (reviewed 2026-10-08): ECMAScript — Array.prototype.sort and CompareArrayElements: https://tc39.es/ecma262/multipage/indexed-collections.html#sec-array.prototype.sort

Sources and verification

Sources checked:

This editorial answer was prepared by KitForma with AI assistance. It is not presented as a real member question or an independent user review. Check the sources and the result with your own file; report corrections in the discussion.

Discussion and community answers

Loading community…
KitForma

What would you like to do?