Created
December 5, 2016 12:36
-
-
Save 6pm/ef2832566ea86b28951d4adec01e4682 to your computer and use it in GitHub Desktop.
javascript sort - сортувати за 2 полями в обєкті. Count буде пріоритетнішшим
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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