http://jsfiddle.net/dy8w8r7d/如何通過兩個屬性對json數據進行排序
如何通過兩個屬性對JSON數據進行排序? 下面只排序機能的研究由Episode
..
目標:爲了通過Episode
上升然後通過Sequence
上升
var data = [ { ArticleId: 2, Episode: 1, Type: 0, Sequence: 0 },
{ ArticleId: 1, Episode: 1, Type: 0, Sequence: 1 },
{ ArticleId: 3, Episode: 2, Type: 0, Sequence: 0 },
{ ArticleId: 4, Episode: 2, Type: 0, Sequence: 1 } ];
var data = [ { ArticleId: 2, Episode: 1, Type: 0, Sequence: 1 },
{ ArticleId: 1, Episode: 2, Type: 0, Sequence: 0 },
{ ArticleId: 3, Episode: 1, Type: 0, Sequence: 0 },
{ ArticleId: 4, Episode: 2, Type: 0, Sequence: 1 } ];
function compareEpisodeSequence(a,b) {
if (a.Sequence < b.Sequence)
return -1;
if (a.Sequence > b.Sequence)
return 1;
return 0;
}
data.sort(compareEpisodeSequence);
console.log(data);
只需進行兩次排序。首先按順序排序,然後按照情節排序。 – Archer