排序在不同浏览器中返回不同的结果

时间:2016-06-01 20:32:03

标签: javascript jquery json sorting

function sort(type) {
    $("#parentDiv").empty();
    $.getJSON("raw_data.json", ({ Search }) => {   
        Search.sort((a, b) => a[type] > b[type]);
        console.log(`Sorted by: ${type}`);
        ...code

在不同的浏览器中显示不同的结果,但正确的结果显示在firefox devoloper版本中,错误的结果显示在chrome浏览器中。

1 个答案:

答案 0 :(得分:0)

sort函数应返回一个数字

小于0的数字会将元素A移动到比B

更低的索引

大于0的数字会将元素B移动到比A

更低的索引

如果数字为零,则会将元素保留在同一位置。

有关此处排序功能的更多信息:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

相关问题