[英]Javascript - Issue with sort function on Opera 11.60 and IE 8
I am trying to sort objects by there properties. 我试图按那里的属性对对象进行排序。 I have a problem on Opera and IE with my function.
我在Opera和IE上的功能存在问题。 Till now, I have debugged the problem on Opera at this stage:
到现在为止,我已经在此阶段调试了Opera上的问题:
Pass this code in the console and press shift+enter. 在控制台中传递此代码,然后按Shift + Enter。
var DataArray=["Other","Attitude","Attitude","Delivery/timings","General Performance","Personal Planning","Other"] DataArray=DataArray.sort(function(a,b) { return a<b; }); JSON.stringify(DataArray);
You should get correct result like this: 您应该获得如下正确的结果:
["Personal Planning","Other","Other","General Performance","Delivery/timings","Attitude","Attitude"]
[“个人规划”,“其他”,“其他”,“总体表现”,“交付/时间”,“态度”,“态度”]
Now change the sort function in this way a>b like this and press enter+shift to exucute it. 现在以这种方式更改排序功能a> b,然后按enter + shift将其执行。
var DataArray=["Other","Attitude","Attitude","Delivery/timings","General Performance","Personal Planning","Other"] DataArray=DataArray.sort(function(a,b) { return a>b; }); JSON.stringify(DataArray);
My result is: 我的结果是:
["Attitude","Delivery/timings","Attitude","General Performance","Other","Other","Personal Planning"]
[“态度”,“交付/时间”,“态度”,“一般表现”,“其他”,“其他”,“个人计划”]
Note the first, the second and the third value?What is going on? 注意第一个,第二个和第三个值吗?
If you execute this in the console "Attitude"="Attitude" it returns true... 如果在控制台“ Attitude” =“ Attitude”中执行此操作,则返回true ...
Any ideas? 有任何想法吗?
Thanks in advance. 提前致谢。
EDIT: And the part with the IE: 编辑:和与IE部分:
CODE: 码:
var DataArray=['Other','Attitude','Attitude','Delivery/timings','General Performance','Personal Planning','Other'];
DataArray=DataArray.sort(function(a,b)
{
return a<b;
});
prompt('',DataArray);
Result(correct):Personal Planning,Other,Other,General Performance,Attitude,Attitude,Delivery/timings 结果(正确):个人计划,其他,其他,总体表现,态度,态度,交付/时间
CODE: 码:
var DataArray=['Other','Attitude','Attitude','Delivery/timings','General Performance','Personal Planning','Other']; var DataArray = ['其他','态度','态度','交付/时间','总体表现','个人规划','其他'];
DataArray=DataArray.sort(function(a,b)
{
return a>b;
});
prompt('',DataArray);
RESULT (incorrect): Attitude,Attitude,Other,Delivery/timings,General Performance,Other,Personal Planning 结果(不正确):态度,态度,其他,交付/时间,总体表现,其他,个人计划
SOLUTION: 解:
sortableArray=sortableArray.sort(function(a,b)
{
if(a.Category>b.Category)
{
return 1;
}
if(a.Category<b.Category)
{
return -1;
}
return 0;
});
To sort in descending way the data use reverse() function. 要对数据进行降序排序,请使用reverse()函数。
Thank you for the help. 感谢您的帮助。 Especially to @nnnnnn
特别是@nnnnnn
This isn't a problem with any particular browser, it is a problem with not having read the .sort()
function documentation . 这对于任何特定的浏览器而言都不是问题,而对于未阅读
.sort()
函数文档的情况而言 ,则是一个问题。 The callback you pass to .sort()
is not supposed to return a boolean, it is supposed to return a number that is: 您传递给
.sort()
的回调不应该返回布尔值,而应该返回一个数字:
Your sorting function returns a boolean value, while it should return an integer. 您的排序函数返回一个布尔值,而它应该返回一个整数。 While
1
can be confused as true
and 0
as false
, this shouldn't be something that you rely your code logic on. 虽然
1
可以混淆为true
而0
可以混淆为false
,但这不应该是您依赖代码逻辑的东西。 The function should have 3 return cases : 该函数应具有3个返回情况:
< 0
if a > b
< 0
如果a > b
> 0
if a < b
> 0
如果a < b
== 0
if a == b
a == b
== 0
So you should use sort your array like this : 所以你应该像这样对数组进行排序:
DataArray = DataArray.sort(function(a,b){
return a>b ? 1 : a<b ? -1 : 0;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.