Sorting an array of decimal & whole numbers

zoosrc

I can't seem to figure this one out.

var arr = [2.62, 111.05, 1.05]
arr.sort(); 

This returns [1.05, 111.05, 2.62], but I'm expecting [1.05, 2.62, 111.05].

How can this be achieved? I've read a bit about writing a custom sort to split the "decimal" but haven't managed to have any success.

styvane

By default sorting is alphabetically. You need to pass a function to the sort method

arr.sort(function(a, b){return a-b;});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Make decimal point to whole numbers

From Dev

Make decimal point to whole numbers

From Dev

Sorting 'numbers' with multiple decimal points

From Dev

Get all whole and decimal numbers from string

From Dev

Sorting array of string by numbers

From Dev

Difference between inputing whole numbers and decimal numbers in C

From Dev

Converting array of binary numbers to decimal

From Dev

Sorting numbers and then letters in a string array

From Dev

sorting the array and finding repeated numbers

From Dev

Excel number format adds decimal point for whole numbers

From Dev

Sorting ranked data in Excel while listing duplicates as different whole numbers

From Dev

Numpy: Check if float array contains whole numbers

From Dev

How sorting parts of array my cause the whole array to be sorted

From Dev

How to drop decimal place on numbers in a JSON array?

From Dev

Searching for numbers with a comma as a decimal point in an array

From Dev

Why does Excel parse numbers with more than two decimal places as whole numbers?

From Java

Sorting an array of strings by arbitrary numbers of substrings

From Dev

Sorting an array of version numbers in C++

From Dev

Bug sorting negative numbers in an array (double)

From Dev

Sorting even and odd numbers inside an array

From Dev

Largest 5 in array of 10 numbers without sorting

From Dev

Sorting an array of filenames containing strings with numbers

From Dev

Time complexity for sorting an array of binary numbers

From Dev

Bug sorting negative numbers in an array (double)

From Dev

Trouble sorting an array of numbers into evens and odds

From Dev

Sorting even and odd numbers inside an array

From Dev

Sorting-(smallest 2 numbers in an array)

From Dev

Finding the 3 largest numbers in an array/list of n numbers without the sorting

From Dev

Trouble sorting an decimal array with bubblesort. c#

Related Related

  1. 1

    Make decimal point to whole numbers

  2. 2

    Make decimal point to whole numbers

  3. 3

    Sorting 'numbers' with multiple decimal points

  4. 4

    Get all whole and decimal numbers from string

  5. 5

    Sorting array of string by numbers

  6. 6

    Difference between inputing whole numbers and decimal numbers in C

  7. 7

    Converting array of binary numbers to decimal

  8. 8

    Sorting numbers and then letters in a string array

  9. 9

    sorting the array and finding repeated numbers

  10. 10

    Excel number format adds decimal point for whole numbers

  11. 11

    Sorting ranked data in Excel while listing duplicates as different whole numbers

  12. 12

    Numpy: Check if float array contains whole numbers

  13. 13

    How sorting parts of array my cause the whole array to be sorted

  14. 14

    How to drop decimal place on numbers in a JSON array?

  15. 15

    Searching for numbers with a comma as a decimal point in an array

  16. 16

    Why does Excel parse numbers with more than two decimal places as whole numbers?

  17. 17

    Sorting an array of strings by arbitrary numbers of substrings

  18. 18

    Sorting an array of version numbers in C++

  19. 19

    Bug sorting negative numbers in an array (double)

  20. 20

    Sorting even and odd numbers inside an array

  21. 21

    Largest 5 in array of 10 numbers without sorting

  22. 22

    Sorting an array of filenames containing strings with numbers

  23. 23

    Time complexity for sorting an array of binary numbers

  24. 24

    Bug sorting negative numbers in an array (double)

  25. 25

    Trouble sorting an array of numbers into evens and odds

  26. 26

    Sorting even and odd numbers inside an array

  27. 27

    Sorting-(smallest 2 numbers in an array)

  28. 28

    Finding the 3 largest numbers in an array/list of n numbers without the sorting

  29. 29

    Trouble sorting an decimal array with bubblesort. c#

HotTag

Archive