3

If I have an array like:

["23", "765", "sfasf", "2.3E-3", "2.3cE-3"]

How can I order it so numbers(decimals, floats or scientific notation) are ordered ascending and after those strings that aren't numbers(for example "sfasf" or "2.3cE-3")?

Expected order of the example array:

["2.3E-3", "23", "765", "2.3cE-3", "sfasf"]

The order of the strings that can't be converted to numbers doesn't matter, they just must be at the end.

Solution from answer:

 $scope.cleanAndOrder = function(dbo, fieldName) {
    var textareaId = "textarea"+"_"+fieldName ;
    var textarea= document.getElementById(textareaId);

    //(+b && (+a!=a)) : return true (converted to 1) if b is a number and a isn't
    //(a-b) : then compare the numbers if the first comparaison isn't enough
    textarea.value = dbo.attributes[fieldName].sort(function(a,b){ return (+b && !+a) || (a-b) }).join("\n");
    var lines = textarea.value.split("\n");
    textarea.setAttribute('rows', lines.length +2);
 }
4
  • 2
    Use lo-dash or underscore :) Commented May 29, 2015 at 11:37
  • 1
    Thanks for the comment could you point out a particular function, please? Commented May 29, 2015 at 11:40
  • Do you want a specific order for strings ? Commented May 29, 2015 at 11:43
  • No the string order was irrelevant in this case! Commented May 29, 2015 at 11:44

2 Answers 2

5

You can do

var arr = arr.sort(function(a,b){ return ((+b==b) && (+a!=a)) || (a-b) })

The idea is to make two comparisons:

  • (+b==b) && (+a!=a) : return true (converted to 1) if b is a number and a isn't
  • a-b : then compare the numbers if the first comparaison isn't enough

More in depth : +a converts a to a number. It is equal (for ==, not for ===) to a when and only when +a is a number (remember, NaN isn't equal to NaN).

Sign up to request clarification or add additional context in comments.

10 Comments

can u please add some explanation for the code u posted?
Awesome! works like a charm I will accept your answer in a few min. Thanks.
@DenysSéguret what a-b for?
@Alex It's just the difference. If you want to sort numbers you do [3, 32, -4, 21].sort(function(a,b){ return a-b })
@DenysSéguret Hey thanks again but it swops some values if I run the operation consecutively with the following values: 0.00000000067 asdas 6.14475066 2323.324324 234E12 2.11220065 2.3EE-3 90.96dsfsdf936856 dsfsdf --2.3E-3 38.2046219'
|
0

sort function accept a function comparaison as parameter.

Define your

function compareFct(a, b) {
    if (isNaN(a)) {
        if (isNaN(b)) {  // a and b are strings
            return a.localeCompare(b);
        } else {         // a string and b number
            return 1;  // a > b
        }
    } else {
        if (isNaN(b)) {  // a number and b string
            return -1;  // a < b
        } else {         // a and b are numbers
            return parseFloat(a) - parseFloat(b);
        }
    }
}

and use it like

yourArray.sort(compareFct);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.