• nintendiator@feddit.cl
    link
    fedilink
    English
    arrow-up
    1
    ·
    1 year ago

    Honestly this being javascript I expected the answer to be

    [4, 1, 100000, 30, 21]
    

    (sorted alphabetically by name)

  • Konlanx@feddit.de
    link
    fedilink
    arrow-up
    0
    ·
    1 year ago

    This is due to the default sorter in JavaScript sorting by the string value. The reason for that is that this won’t create errors at runtime. Since JavaScript does not have types, this is the safest way of sorting.

    This works:

    const unsorted = [1, 100000, 21, 30, 4]
    const sorted = unsorted.sort((a, b) => a - b)