我有一个对象列表,我希望基于类型字符串的字段attr进行排序。我试着用
list.sort(function (a, b) {
return a.attr - b.attr
})
但发现-似乎不适用于JavaScript中的字符串。我如何能排序基于类型字符串的属性的对象列表?
我有一个对象列表,我希望基于类型字符串的字段attr进行排序。我试着用
list.sort(function (a, b) {
return a.attr - b.attr
})
但发现-似乎不适用于JavaScript中的字符串。我如何能排序基于类型字符串的属性的对象列表?
当前回答
直接使用sort(),不带任何-或<
Const区域=['山','海滩','沙漠','山'] console.log (areas.sort ()) //以降序方式打印 .reverse console.log (areas.sort () ())
其他回答
<!doctype html>
<html>
<body>
<p id = "myString">zyxtspqnmdba</p>
<p id = "orderedString"></p>
<script>
var myString = document.getElementById("myString").innerHTML;
orderString(myString);
function orderString(str) {
var i = 0;
var myArray = str.split("");
while (i < str.length){
var j = i + 1;
while (j < str.length) {
if (myArray[j] < myArray[i]){
var temp = myArray[i];
myArray[i] = myArray[j];
myArray[j] = temp;
}
j++;
}
i++;
}
var newString = myArray.join("");
document.getElementById("orderedString").innerHTML = newString;
}
</script>
</body>
</html>
我为这个问题困扰了很久,所以我最终研究了这个问题,并给了你一个冗长的原因,为什么事情是这样的。
来自规范:
Section 11.9.4 The Strict Equals Operator ( === )
The production EqualityExpression : EqualityExpression === RelationalExpression
is evaluated as follows:
- Let lref be the result of evaluating EqualityExpression.
- Let lval be GetValue(lref).
- Let rref be the result of evaluating RelationalExpression.
- Let rval be GetValue(rref).
- Return the result of performing the strict equality comparison
rval === lval. (See 11.9.6)
现在我们看11.9.6
11.9.6 The Strict Equality Comparison Algorithm
The comparison x === y, where x and y are values, produces true or false.
Such a comparison is performed as follows:
- If Type(x) is different from Type(y), return false.
- If Type(x) is Undefined, return true.
- If Type(x) is Null, return true.
- If Type(x) is Number, then
...
- If Type(x) is String, then return true if x and y are exactly the
same sequence of characters (same length and same characters in
corresponding positions); otherwise, return false.
就是这样。应用于字符串的三重等号运算符如果参数是完全相同的字符串(相同的长度和对应位置的相同字符)则返回true。
所以===将在我们试图比较可能来自不同来源的字符串,但我们知道它们最终将具有相同的值的情况下工作——这是我们代码中内联字符串的一个足够常见的场景。例如,如果我们有一个名为connection_state的变量,并且我们希望知道它现在处于以下状态中的哪一种['connecting', 'connected', 'disconnecting', 'disconnected'],我们可以直接使用===。
但还有更多。就在11.9.4上面,有一个简短的说明:
NOTE 4
Comparison of Strings uses a simple equality test on sequences of code
unit values. There is no attempt to use the more complex, semantically oriented
definitions of character or string equality and collating order defined in the
Unicode specification. Therefore Strings values that are canonically equal
according to the Unicode standard could test as unequal. In effect this
algorithm assumes that both Strings are already in normalized form.
嗯。现在该做什么?外部获得的字符串可能,而且很可能会是奇怪的统一码,而我们温和的===不会公正地对待它们。在localeCompare来拯救:
15.5.4.9 String.prototype.localeCompare (that)
...
The actual return values are implementation-defined to permit implementers
to encode additional information in the value, but the function is required
to define a total ordering on all Strings and to return 0 when comparing
Strings that are considered canonically equivalent by the Unicode standard.
我们现在可以回家了。
tl; diana;
要比较javascript中的字符串,请使用localeCompare;如果您知道字符串没有非ascii组件,因为它们是,例如,内部程序常量,那么===也适用。
因为在javascript中可以直接比较字符串,所以这样做就可以了
list.sort(function (a, b) {
return a.attr < b.attr ? -1: 1;
})
这比使用更有效率
return a.attr > b.attr ? 1: -1;
因为如果元素具有相同的attr (a.t attr == b.t attr),排序函数将毫无理由地交换两者。
例如
var so1 = function (a, b) { return a.atr > b.atr ? 1: -1; };
var so2 = function (a, b) { return a.atr < b.atr ? -1: 1; }; // better
var m1 = [ { atr: 40, s: "FIRST" }, { atr: 100, s: "LAST" }, { atr: 40, s: "SECOND" } ].sort (so1);
var m2 = [ { atr: 40, s: "FIRST" }, { atr: 100, s: "LAST" }, { atr: 40, s: "SECOND" } ].sort (so2);
// m1 sorted but ...: 40 SECOND 40 FIRST 100 LAST
// m2 more efficient: 40 FIRST 40 SECOND 100 LAST
这里应该使用>或<和==。所以解决方案是:
list.sort(function(item1, item2) {
var val1 = item1.attr,
val2 = item2.attr;
if (val1 == val2) return 0;
if (val1 > val2) return 1;
if (val1 < val2) return -1;
});
在第一个问题的操作中,您正在执行以下操作:
item1.attr - item2.attr
所以,假设这些是数字(即item1。Attr = "1", item2。attr =" 2")您仍然可以使用"==="运算符(或其他严格求值符),只要您确保类型。以下方法应该有效:
return parseInt(item1.attr) - parseInt(item2.attr);
如果它们是字母和数字,那么使用localCompare()。