问题描述
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浏览器中会显示错误的结果.
Different results are shown in different browsers, but the correct one is showing in firefox devoloper edition and wrong results are shown in chrome browser.
推荐答案
sort函数应返回数字,
The sort function should return a number,
小于0的数字会将元素A移到比B低的索引上
a number less than 0 will move element A to a lower index than B
大于0的数字会将元素B移到比A低的索引处
a number greater than 0 will move element B to a lower index than A
如果数字为零,则元素将保留在同一位置.
if the number is zero it will leave the elements at the same place.
有关此处的排序功能的更多信息: https ://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
More about the sort function here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
这篇关于在不同的浏览器中返回不同结果的排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!