[英]How to sort an array in Javascript like C#?
I would like to sort an array in JavaScript as asc. 我想对JavaScript中的asc数组进行排序。 After I used sort method, the result is like below.
使用排序方法后,结果如下。
[ '123, '12', '1A1', '1A', '1a', 'A1', 'A2', 'AB', 'A', 'Ab', 'a1', 'a2', 'aB', 'ab' ]
When I'm using sort Array.Sort()
by C#, the result is like below. 当我通过C#使用sort
Array.Sort()
,结果如下所示。
{ "12", "123", "1a", "1A", "1A1", "A", "a1", "A1", "a2", "A2", "ab", "aB", "Ab", "AB" }
Actually the C# sort way is what I want, so how to implement it in JavaScript? 实际上,我想要的是C#排序方式,那么如何在JavaScript中实现呢? Has anyone completed this task before?
有人完成过此任务吗? A comparator function is really appreciate.
比较器功能真的很值得。
Array.prototype.sort
uses string comparison by default. Array.prototype.sort
默认使用字符串比较。 It seems you want to compare case insensitive - you could pass in a comparator function that uses toLowerCase
(but beware of the difference with .toLocaleLowerCase
!): 似乎您想比较不区分大小写的代码-您可以传入一个使用
toLowerCase
的比较器函数(但要注意与.toLocaleLowerCase
的区别!):
a.sort(function(a, b) {
a = a.toLowerCase(); b = b.toLowerCase();
return a > b ? 1 : a < b ? -1 : 0;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.