Skip to the content

Numbers that arrived as text

Problem

A survey app stores every answer as text: even if the person types 7, what gets saved is "7".

Write a function that takes a list of numbers written as text and returns a new list with them sorted from the smallest to the largest, and already turned into numbers.

With "4", "12", "45", "7", "0" and "100" you return 0, 4, 7, 12, 45 and 100. The rules: a repeated number stays repeated, you drop nothing. There can be negative numbers, written with their sign. If the list arrives empty, you return an empty list.

Careful with sorting the texts as they are: compared as text, "10" lands before "9", because they are compared character by character. It pays to convert before sorting, or to tell whatever sorts them which criterion to compare by.

Examples

  • The answers from the form

    ["4", "12", "45", "7", "0", "100"] → [0, 4, 7, 12, 45, 100]

  • The 9 and the 10

    ["10", "9", "100", "20"] → [9, 10, 20, 100]

  • With repeats

    ["3", "1", "3", "2", "1"] → [1, 1, 2, 3, 3]

Besides these, the challenge has hidden tests that are revealed when you submit your solution.

You start with this

Python

def sort_numbers(texts):
    pass

JavaScript

function sortNumbers(texts) {
}
Solve this challenge

It opens in your browser, with the editor and the tests. This challenge is part of the full plan; the O(1) and O(log n) ones are free.

More O(n log n) challenges

See all challenges →