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
2 Answers
Reset to default 5You 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)
: returntrue
(converted to1
) ifb
is a number anda
isn'ta-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
版权声明:本文标题:Javascript sort() array of mixed data type - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745155156a2645124.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论