gpt4 book ai didi

在对对象数组内的数字进行排序时,JavaScript 排序无法按预期工作

转载 作者:行者123 更新时间:2023-12-01 01:17:38 29 4
gpt4 key购买 nike

我试图按对象内部的属性对对象数组进行排序,而 js 没有很好地对其进行排序。这是我使用的代码和我尝试排序的数组:

const json = [ 
{ code: '40828',
distance: { text: '2.3 km', value: 2309 },
duration: { text: '14 mins', value: 831 },
},
{ code: '43343',
distance: { text: '35.4 km', value: 35432 },
duration: { text: '1 hour 27 mins', value: 5242 },
},
{ code: '40539',
distance: { text: '26.1 km', value: 26134 },
duration: { text: '1 hour 38 mins', value: 5885 },
},
{ code: '4840',
distance: { text: '2.3 km', value: 2340 },
duration: { text: '17 mins', value: 1044 },
},
{ code: '32725',
distance: { text: '6.2 km', value: 6218 },
duration: { text: '34 mins', value: 2011 },
},
{ code: '39608',
distance: { text: '3.6 km', value: 3620 },
duration: { text: '22 mins', value: 1299 },
},
{ code: '50913',
distance: { text: '4.7 km', value: 4707 },
duration: { text: '35 mins', value: 2112 },
},
{ code: '43879',
distance: { text: '10.1 km', value: 10065 },
duration: { text: '49 mins', value: 2938 },
},
{ code: '35606',
distance: { text: '3.0 km', value: 2965 },
duration: { text: '19 mins', value: 1168 },
},
{ code: '4377',
distance: { text: '4.5 km', value: 4524 },
duration: { text: '24 mins', value: 1439 },
},
{ code: '41519',
distance: { text: '23.9 km', value: 23940 },
duration: { text: '1 hour 5 mins', value: 3872 },
}
]

console.log(json.sort((a, b) => a.duration.value > b.duration.value));

我正在尝试按 distance.value 对其进行排序我得到这个结果:

[ { code: '39608',
distance: { text: '3.6 km', value: 3620 },
duration: { text: '22 mins', value: 1299 } },
{ code: '40828',
distance: { text: '2.3 km', value: 2309 },
duration: { text: '14 mins', value: 831 } },
{ code: '4840',
distance: { text: '2.3 km', value: 2340 },
duration: { text: '17 mins', value: 1044 } },
{ code: '35606',
distance: { text: '3.0 km', value: 2965 },
duration: { text: '19 mins', value: 1168 } },
{ code: '4377',
distance: { text: '4.5 km', value: 4524 },
duration: { text: '24 mins', value: 1439 } },
{ code: '32725',
distance: { text: '6.2 km', value: 6218 },
duration: { text: '34 mins', value: 2011 } },
{ code: '50913',
distance: { text: '4.7 km', value: 4707 },
duration: { text: '35 mins', value: 2112 } },
{ code: '43879',
distance: { text: '10.1 km', value: 10065 },
duration: { text: '49 mins', value: 2938 } },
{ code: '41519',
distance: { text: '23.9 km', value: 23940 },
duration: { text: '1 hour 5 mins', value: 3872 } },
{ code: '43343',
distance: { text: '35.4 km', value: 35432 },
duration: { text: '1 hour 27 mins', value: 5242 } },
{ code: '40539',
distance: { text: '26.1 km', value: 26134 },
duration: { text: '1 hour 38 mins', value: 5885 } } ]

所以结果数组中的所有内容看起来都很好(并且排序良好),除了第一个元素,我想理解为什么?值 831 应该是数组中的第一个,但由于某种原因是第二个,第一个是 1299。任何人都可以解释一下,也许我做错了什么?

提前致谢🍻

最佳答案

您很接近,但您的排序略有偏差。该排序不会查找 bool 返回类型,而是查找整数。所以你需要像这样修改它: json.sort((a,b) => a.distance.value - b.distance.value);

关于在对对象数组内的数字进行排序时,JavaScript 排序无法按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54593566/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com