admin管理员组

文章数量:1414621

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 pare the numbers if the first paraison 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);
 }

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 pare the numbers if the first paraison 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);
 }
Share Improve this question edited May 29, 2015 at 12:27 Al Ex Tsm asked May 29, 2015 at 11:33 Al Ex TsmAl Ex Tsm 2,1022 gold badges31 silver badges50 bronze badges 4
  • 2 Use lo-dash or underscore :) – Andrey Popov Commented May 29, 2015 at 11:37
  • 1 Thanks for the ment could you point out a particular function, please? – Al Ex Tsm Commented May 29, 2015 at 11:40
  • Do you want a specific order for strings ? – Denys Séguret Commented May 29, 2015 at 11:43
  • No the string order was irrelevant in this case! – Al Ex Tsm Commented May 29, 2015 at 11:44
Add a ment  | 

2 Answers 2

Reset to default 5

You can do

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

The idea is to make two parisons:

  • (+b==b) && (+a!=a) : return true (converted to 1) if b is a number and a isn't
  • a-b : then pare the numbers if the first paraison 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).

sort function accept a function paraison as parameter.

Define your

function pareFct(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(pareFct);

本文标签: Javascript sort() array of mixed data typeStack Overflow