Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 6pm/ef2832566ea86b28951d4adec01e4682 to your computer and use it in GitHub Desktop.
Save 6pm/ef2832566ea86b28951d4adec01e4682 to your computer and use it in GitHub Desktop.
javascript sort - сортувати за 2 полями в обєкті. Count буде пріоритетнішшим
var data = [
{ count: 'phone', year: '1956' },
{ count: 'phone', year: '1971' },
{ count: 'text', year: '1989' },
{ count: '33', year: '1932' },
{ count: '33', year: '1912' },
{ count: 'text', year: '1954' },
{ count: 'phone', year: '1920' },
{ count: '33', year: '3444' }
];
data.sort(function(a,b){
return a['count']<b['count']?-1:(a['count']>b['count']?1:(a['year']<b['year']?-1:1));
});
var i;
for (i = 0; i < data.length; i++) {
document.write("<p>count:" + data[i].count +
" year:" + data[i].year + "</p>");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment