【发布时间】:2014-07-30 06:22:56
【问题描述】:
我有一堆存储在 JavaScript 对象数组中的歌曲。它看起来像这样:
var library = [
{ "title": "40 Years Later", "album": "Tears of Steel", "year": 2012, "track": 1, "disk": 1, "time": 31 },
{ "title": "The Dome", "album": "Tears of Steel", "year": 2012, "track": 2, "disk": 1, "time": 311 },
{ "title": "The Battle", "album": "Tears of Steel", "year": 2012, "track": 3, "disk": 1, "time": 123 },
{ "title": "End Credits", "album": "Tears of Steel", "year": 2012, "track": 4, "disk": 1, "time": 103 },
{ "title": "The Wires", "album": "Elephants Dream", "year": 2006, "track": 1, "disk": 1, "time": 75 },
{ "title": "Typewriter Dance", "album": "Elephants Dream", "year": 2006, "track": 2, "disk": 1, "time": 70 },
{ "title": "The Safest Place", "album": "Elephants Dream", "year": 2006, "track": 3, "disk": 1, "time": 45 },
{ "title": "Emo Creates", "album": "Elephants Dream", "year": 2006, "track": 4, "disk": 1, "time": 60 },
{ "title": "End Title", "album": "Elephants Dream", "year": 2006, "track": 5, "disk": 1, "time": 91 },
{ "title": "Teaser Music", "album": "Elephants Dream", "year": 2006, "track": 6, "disk": 1, "time": 75 },
{ "title": "Ambience", "album": "Elephants Dream", "year": 2006, "track": 7, "disk": 1, "time": 110 },
{ "title": "Snow Fight", "album": "Sintel", "year": 2010, "track": 1, "disk": 1, "time": 107 },
{ "title": "Finding Scales / Chicken Run", "album": "Sintel", "year": 2010, "track": 2, "disk": 1, "time": 107 },
{ "title": "The Ziggurat", "album": "Sintel", "year": 2010, "track": 3, "disk": 1, "time": 78 },
{ "title": "Expedition", "album": "Sintel", "year": 2010, "track": 4, "disk": 1, "time": 93 },
{ "title": "Dragon Blood Tree", "album": "Sintel", "year": 2010, "track": 5, "disk": 1, "time": 47 },
{ "title": "Cave Fight / Lament", "album": "Sintel", "year": 2010, "track": 6, "disk": 1, "time": 145 },
{ "title": "I Move On (Sintel's Song)", "album": "Sintel", "year": 2010, "track": 7, "disk": 1, "time": 169 },
{ "title": "Circling Dragons", "album": "Sintel", "year": 2010, "track": 8, "disk": 1, "time": 28 },
{ "title": "Trailer Music", "album": "Sintel", "year": 2010, "track": 9, "disk": 1, "time": 44 }
];
我需要按字母和数字顺序对属性进行排序。 StackOverflow 上有很多文章和问题涉及按单个值排序,有些似乎涵盖多个值但它们没有顺序或重要性。
我需要按几个值对每个对象(歌曲)进行排序,其中每个值的重要性等级较低。例如:
album名称 (字母) >disk数字 (数字) >track数字 (数字) > @987654325 @(按字母顺序) > 等等
这意味着专辑放在一起,每个专辑都按字母顺序排列。在每张专辑中都是按磁盘编号排序的歌曲,因此磁盘 1 中的所有歌曲都在顶部,然后是磁盘 2 中的所有歌曲,依此类推。在每个磁盘编号分组中都是按曲目编号排序的歌曲。如果有多首歌曲具有相同的曲目编号,或者没有曲目编号,它们将按歌曲名称按字母顺序排序。如果track title也一样,可以赋予更多的属性。
大写无关紧要,它应该像正常的字母排序那样排序(如果它以数字开头,那将排在字母之前,特殊字符将像传统排序一样占据一席之地)。
我曾尝试使用此代码(它具有硬编码的排序值且缺少磁盘编号),但它仅按代码中最内层的属性(轨道号)进行排序。
library.sort(function (a, b) {
if (a.album === b.album) {
if (a.track === b.track) {
var x = a.title.toLowerCase();
var y = b.title.toLowerCase();
return x < y ? -1 : x > y ? 1 : 0;
}
var x = a.track;
var y = b.track;
return x < y ? -1 : x > y ? 1 : 0;
}
return a.album.toLowerCase() - b.album.toLowerCase();
});
我正在寻找一个函数,该函数将重新排列 library 数组,以便按照上述输入的内容对其进行排序:
sort(library, [ "album", "disk", "track", "title" ]);
单个值也应该能够按降序排序,可能类似于-album 或["album", true]。语法灵活。
【问题讨论】:
标签: javascript arrays json sorting