Leo*_*ssi 0 javascript arrays sorting
我知道有一个类似的问题,Javascript - 在数组的一个属性上按字母顺序对数组中的对象进行排序,但是当我使用此方法逐步运行时,它会跳过排序.
我有一个对象数组,在控制台中看起来像这样:
0:{id: "3645256536754", name: "john", description: "man", children: Array(0)}
1:{id: "8672092533958", name: "alex", description: "man", children: Array(2)}
Run Code Online (Sandbox Code Playgroud)
我必须做一个forEach,但在我必须确定它是基于name属性的字母顺序之前.
我是这样做的:
myArray.sort(function (a, b) {
var textA = a.name.toUpperCase();
var textB = b.name.toUpperCase();
return (textA < textB) ? -1 : (textA > textB) ? 1 : 0;
})
Run Code Online (Sandbox Code Playgroud)
当我一步一步地运行它时,它跳过这段代码,我不知道为什么.有什么建议?
使用@ gurvinder372的评论作为影响,您应该执行以下操作:
myArray.sort(function (a, b) {
var textA = a.name.toUpperCase();
var textB = b.name.toUpperCase();
return textA.localeCompare(textB);
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4871 次 |
| 最近记录: |