Sorting Mixed Data Types: Numbers, Text, and Dates in One List

May 28, 20266 min read

The Mixed-Type Sorting Problem

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.

Sort a list like 100, apple, 42, banana, 7 alphabetically and you get 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.

Understanding how mixed-type sorting works helps you predict results and choose the right approach.

Type Comparison Rules

When a sorting algorithm compares two values of different types, it needs a rule. There are three common strategies:

StrategyHow It WorksExample Order
All-as-textConvert everything to strings first100, 42, 7, apple, banana
Type prioritySort by type group, then within type7, 42, 100, apple, banana
Error on mismatchReject lists with mixed typesThrows error

Most practical tools use type priority: numbers sort together first (by value), then text (alphabetically), then dates (chronologically). This produces the most intuitive result.

Numbers vs. Text Strings

The most common mixed-type scenario is numbers and text in the same list.

Alphabetical sort compares character codes, so 100 comes before 42 because 1 < 4. This is technically correct but visually wrong.

Numeric sort treats 100 and 42 as numbers and orders them by value: 42, 100. Text entries like apple cannot be parsed as numbers, so they fall to the end or get grouped separately.

Practical Example

Input:    100, apple, 42, banana, 7, cherry

Type priority sort (numbers first):
         7, 42, 100, apple, banana, cherry

Type priority sort (text first):
         apple, banana, cherry, 7, 42, 100

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.

Sorting Dates Alongside Other Types

Dates add another layer. The string 2026-01-15 looks like text to a naive sort, but semantically it represents a point in time.

If your tool recognizes date formats (ISO 8601, MM/DD/YYYY, etc.), it can parse them into timestamps and sort chronologically. If not, dates sort as text — which happens to work correctly for ISO format (YYYY-MM-DD) but fails for formats like 01/15/2026.

ISO dates sorted as text (correct):
  2025-03-01, 2025-12-01, 2026-01-15

US dates sorted as text (wrong):
  01/15/2026, 03/01/2025, 12/01/2025

Recommendation

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.

Handling Blank Lines and Empty Values

Blank lines and empty cells are a special type. They carry no information but disrupt sorting:

  • Sorted to the top — blanks appear first in ascending order (empty string sorts before any character)
  • Sorted to the bottom — some tools push blanks to the end regardless of direction
  • Removed — the cleanest option when blanks serve no purpose

Most list sorters include a "remove blank lines" option. Use it unless blanks are intentional separators.

Sorting Strategy by Data Composition

Data CompositionRecommended Approach
All numbersNumeric sort
All textAlphabetical sort (case-insensitive)
Numbers + textType priority sort (numbers first)
Numbers + datesNumeric sort after converting dates to timestamps
Mixed numbers, text, datesType priority with date parsing
Versions (1.2.10)Natural sort or version sort

Programming Language Approaches

Python

mixed = ['100', 'apple', '42', 'banana', '7']
sorted_list = sorted(mixed, key=lambda x: (not x.isdigit(), int(x) if x.isdigit() else x.lower()))
# Result: ['7', '42', '100', 'apple', 'banana']

The key function assigns a tuple: (False, numeric_value) for numbers and (True, lowercase_text) for strings. Python sorts tuples element-by-element, so all numbers (with False) come before all text (with True).

JavaScript

const mixed = ['100', 'apple', '42', 'banana', '7'];
mixed.sort((a, b) => {
  const aNum = Number(a), bNum = Number(b);
  const aIsNum = !isNaN(aNum), bIsNum = !isNaN(bNum);
  if (aIsNum && bIsNum) return aNum - bNum;
  if (aIsNum) return -1;
  if (bIsNum) return 1;
  return a.localeCompare(b);
});
// Result: ['7', '42', '100', 'apple', 'banana']

Key Takeaways

  • Mixed-type sorting requires a type priority rule — numbers first, then text, then dates — to produce intuitive results
  • Alphabetical sort on mixed data treats numbers as text, producing misleading order (100 before 42)
  • Dates should be in ISO 8601 format if your tool cannot parse date strings
  • Blank lines should typically be removed before sorting to avoid cluttering the result
  • In code, a custom key or comparator that checks types gives you full control over the sort order

Try It Yourself

Paste any mixed list into our List Sorter and instantly see it organized. Toggle between alphabetical and numeric modes to compare results side by side.