javascript - 比较多个字段时排序方法如何工作
问题描述
我正在解决一个编程问题。
有一件事我无法理解,那就是排序。
首先,向您展示代码。
var sumCount = { classic: 1450, pop: 3100 };
var songs = [
{ genre: 'classic', count: 500, index: 0 },
{ genre: 'pop', count: 600, index: 1 },
{ genre: 'classic', count: 150, index: 2 },
{ genre: 'classic', count: 800, index: 3 },
{ genre: 'pop', count: 2500, index: 4 }
]
var sortedSongs = songs.sort((a,b)=>{
if(a.genre !== b.genre) {
return sumCount[b.genre] - sumCount[a.genre];
}
if(a.count !== b.count) {
return b.count - a.count;
}
return a.index - b.index;
})
[![在此处输入图像描述][1]][1]
在这段代码中,sort 方法中的函数是比较流派、计数、索引。这不仅仅是一个像下面的代码。通常,我使用如下代码的排序方法。compare函数只比较数组值。因此值将逐步从低变为高。
[1,3,4].sort((a,b) => a - b));
但我不明白歌曲的价值是如何通过比较许多领域来排序的。你们能不能给我一些提示来唤醒我的大脑,这样我才能理解它......
谢谢。
编辑
这是我在 comparefunction 返回之前记录的步骤。
- 差异类型,交换:真,索引:1 0
- 差异类型,交换:假,索引:2 1
- 差异计数,交换:假,索引:2 0
- 差异计数,交换:真,索引:3 0
- 差异类型,交换:false,索引:3 1
- 差异类型,交换:真,索引:4 0
- 不同类型,交换:true,索引:4 3
- 差异计数,交换:真,索引:4 1
然后我按照上述步骤交换了索引,
1. swap index 1 with 0
songs =[
**{ genre: 'pop', count: 600, index: 1 },**
**{ genre: 'classic', count: 500, index: 0 },**
{ genre: 'classic', count: 150, index: 2 },
{ genre: 'classic', count: 800, index: 3 },
{ genre: 'pop', count: 2500, index: 4 }
]
2. no swap
songs = [
{ genre: 'pop', count: 600, index: 1 },
{ genre: 'classic', count: 500, index: 0 },
{ genre: 'classic', count: 150, index: 2 },
{ genre: 'classic', count: 800, index: 3 },
{ genre: 'pop', count: 2500, index: 4 }
]
3. no swap
songs = [
{ genre: 'pop', count: 600, index: 1 },
{ genre: 'classic', count: 500, index: 0 },
{ genre: 'classic', count: 150, index: 2 },
{ genre: 'classic', count: 800, index: 3 },
{ genre: 'pop', count: 2500, index: 4 }
]
4. swap index 3 with 0
songs = [
{ genre: 'pop', count: 600, index: 1 },
**{ genre: 'classic', count: 800, index: 3 },**
{ genre: 'classic', count: 150, index: 2 },
**{ genre: 'classic', count: 500, index: 0 },**
{ genre: 'pop', count: 2500, index: 4 }
]
5. no swap
songs = [
{ genre: 'pop', count: 600, index: 1 },
{ genre: 'classic', count: 800, index: 3 },
{ genre: 'classic', count: 150, index: 2 },
{ genre: 'classic', count: 500, index: 0 },
{ genre: 'pop', count: 2500, index: 4 }
]
6. swap index 0 with 4
songs = [
{ genre: 'pop', count: 600, index: 1 },
{ genre: 'classic', count: 800, index: 3 },
{ genre: 'classic', count: 150, index: 2 },
**{ genre: 'pop', count: 2500, index: 4 },**
**{ genre: 'classic', count: 500, index: 0 }**
]
7. swap index 3 with 4
songs = [
{ genre: 'pop', count: 600, index: 1 },
**{ genre: 'pop', count: 2500, index: 4 },**
{ genre: 'classic', count: 150, index: 2 },
**{ genre: 'classic', count: 800, index: 3 },**
{ genre: 'classic', count: 500, index: 0 }
]
8. swap index 1 with 4
songs = [
**{ genre: 'pop', count: 2500, index: 4 },**
**{ genre: 'pop', count: 600, index: 1 },**
{ genre: 'classic', count: 150, index: 2 },
{ genre: 'classic', count: 800, index: 3 },
{ genre: 'classic', count: 500, index: 0 }
]
我得到了与我预期不同的结果……这让我很困惑……我错过了什么吗?
简而言之,在这种情况下,选择排序可以找到重复所有元素的最小数字。sort 将从最低到最高对所有元素进行排序。在这个过程中,流派是如何在同一时间排序的?你能告诉我吗?选择,快速,任何排序算法.... [1]:https ://i.stack.imgur.com/BALcb.png
解决方案
您的代码本质上是sumCount[song.genre]
按降序 ( b - a
) 排序的:
if(a.genre !== b.genre) {
return sumCount[b.genre] - sumCount[a.genre];
}
如果它们相等,则按song.count
降序 ( b - a
) 排序:
if(a.count !== b.count) {
return b.count - a.count;
}
如果它们相等,则按song.index
升序 ( a - b
) 排序:
return a.index - b.index;
您可以将其视为以下 (SQL) 伪代码:
ORDER BY sumCount[song.genre] DESC, song.count DESC, song.index ASC
推荐阅读
- asp.net-web-api - 我不能说我的 .net 核心项目中缺少什么……我的 Intellisense 不能正常工作还是什么原因?
- python - Update a table when other tables changed in Django ORM
- php - How to properly format an array?
- python - Speed up date columns conversion (pandas) from string to datetime
- javascript - Create a immutable copy of a property
- python - Python3 multiprocessing a 'for' loop
- java - 由于 build.gradle Android Studio 中的错误,应用程序崩溃
- github - 将史诗游戏帐户与 github 连接
- r - 安装 sparklyr 版本 0.8.4 时出错
- git - 如何丢弃 git 中的所有内容