Sorting strings with decimal points in JavaScript

In JavaScript, sorting strings with decimal points requires converting them to numbers first, since string comparison would treat "10.0" as less than "2.0". This article demonstrates how to sort decimal string arrays properly.

Understanding the Problem

When sorting strings containing decimal numbers, JavaScript's default string comparison doesn't work numerically. For example, ['3.3', '4.4', '2.3', '1.2'] should become ['1.2', '2.3', '3.3', '4.4'], but string sorting would give incorrect results.

The Solution Approach

To solve this problem, we need to:

  1. Convert decimal strings to numbers using parseFloat()
  2. Sort the numbers numerically
  3. Convert back to strings if needed

Method 1: Using parseFloat() and sort()

// Function to sort array of decimal strings
const sortDecimalStrings = (strings) => {
    const numbers = strings.map(parseFloat);
    numbers.sort((a, b) => a - b);
    const sortedStrings = numbers.map((number) => number.toString());
    return sortedStrings;
};

const strings = ['2.6', '1.3', '4', '1.5', '4.77', '3'];
const sortedStrings = sortDecimalStrings(strings);
console.log("Original:", strings);
console.log("Sorted:", sortedStrings);
Original: [ '2.6', '1.3', '4', '1.5', '4.77', '3' ]
Sorted: [ '1.3', '1.5', '2.6', '3', '4', '4.77' ]

Method 2: Direct String Sorting with Numeric Comparison

A more efficient approach that avoids double conversion:

const sortDecimalStringsDirect = (strings) => {
    return strings.sort((a, b) => parseFloat(a) - parseFloat(b));
};

const strings2 = ['10.5', '2.1', '100.0', '9.99'];
const result = sortDecimalStringsDirect(strings2);
console.log("Direct sorting result:", result);
Direct sorting result: [ '2.1', '9.99', '10.5', '100.0' ]

Handling Edge Cases

Consider invalid strings and mixed data:

const sortWithValidation = (strings) => {
    return strings
        .filter(str => !isNaN(parseFloat(str)))  // Remove invalid entries
        .sort((a, b) => parseFloat(a) - parseFloat(b));
};

const mixedStrings = ['3.14', 'invalid', '2.71', '', '1.41'];
const validSorted = sortWithValidation(mixedStrings);
console.log("Filtered and sorted:", validSorted);
Filtered and sorted: [ '1.41', '2.71', '3.14' ]

Comparison of Methods

Method Performance Memory Usage Readability
parseFloat + map Moderate Higher (creates intermediate arrays) High
Direct comparison Better Lower (in-place sorting) High
With validation Moderate Moderate Moderate

Time Complexity

All methods have O(n log n) time complexity due to the sorting operation, where n is the array length. The parseFloat() conversions add O(n) time but don't change the overall complexity.

Conclusion

For sorting decimal strings in JavaScript, use sort() with parseFloat() comparison. The direct comparison method is most efficient, while the map-based approach offers better separation of concerns.

Updated on: 2026-03-15T23:19:00+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements