How can JSON data be sorted based on two attributes?
This code snippet currently only sorts by Episode
..
Purpose: arrange the data by sorting on Episode
in ascending order, followed by sorting on Sequence
in ascending order
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);