如果我有這樣一個數組:的Javascript排序()排列
["23", "765", "sfasf", "2.3E-3", "2.3cE-3"]
如何訂購它使數字(小數,漂浮或科學記數法)是有序的上升和那些AREN串後數字(例如「sfasf」或「2.3cE-3」)?
的示例性陣列的預期的順序:
["2.3E-3", "23", "765", "2.3cE-3", "sfasf"]
不能被轉換爲數字無關緊要字符串的順序,他們只是必須是在末端。從答案
解決方案:
$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);
}
使用LO-劃線或下劃線:) –
感謝你能指出特定功能的註釋,好嗎? –
你想要一個特定的字符串順序嗎? –