Last active
July 11, 2018 09:07
-
-
Save dfkaye/7fcdd946e49dd266cf6969cc2987e2eb to your computer and use it in GitHub Desktop.
secondary or sub sorting an array in JavaScript
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
// 29 Sep 2017 | |
// 4 Oct 2017 - add name object example. | |
// 14 Nov 2017 - pass `a.name, b.name` to `byName()`; rename `test` as `diff`. | |
// 1 Jan 2018 - made some functions more readable. | |
// Inspired by post at https://alistapart.com/article/what-i-talk-about-when-i-talk-about-sorting | |
// which covers an example of sorting by age, but does not ensure items of same age are secondarily | |
// sorted by name in alphabetical order. | |
// This snippet shows how to do that whether names are strings or objects with first and last properties. | |
// See also my other array sorting gist: | |
// https://gist.github.com/dfkaye/2036fb62604b47f80534 | |
var byLastThenFirst = function(a, b) { | |
return a.last.localeCompare(b.last) || a.first.localeCompare(b.first); | |
}; | |
var byName = function(a, b) { | |
if (a.last && a.first) { | |
return byLastThenFirst(a, b); | |
} | |
return a.localeCompare(b); | |
}; | |
var byAge = function(a, b) { | |
return (+a) - (+b); | |
}; | |
var byAgeThenName = function(a, b) { | |
return byAge(a.age, b.age) || byName(a.name, b.name); | |
}; | |
/* name as string */ | |
var flatNames = [ | |
{ name: 'Kei Akamatsu', age: 32 }, | |
{ name: 'Fumiaki Haida', age: 42 }, | |
{ name: 'Tengo Kawana', age: 26 }, | |
{ name: 'Sara Kimoto', age: 11 }, | |
{ name: 'Midori Kobayashi', age: 11 }, | |
{ name: 'Eri Kurono', age: 54 }, | |
{ name: 'Haruki Murakami', age: 6 }, | |
{ name: 'Satoru Nakata', age: 26 }, | |
{ name: 'Yoshio Oumi', age: 26 }, | |
{ name: 'Haruki Oumi', age: 26 }, | |
{ name: 'Miss Saeki', age: 17 }, | |
{ name: 'Yuzuki Shirane', age: 26 }, | |
{ name: 'Kafka Tamura', age: 26 }, | |
{ name: 'Tsukuru Tazaki', age: 32 }, | |
{ name: 'Toru Watanabe', age: 12 } | |
] | |
console.log( | |
flatNames | |
.slice() | |
.sort(byAgeThenName) | |
.map(function(item) { | |
return ''.concat('age: ', item.age, ' name: ', item.name) | |
}) | |
.join('\n') | |
); | |
// should see: | |
/* | |
age: 6 name: Haruki Murakami | |
age: 11 name: Midori Kobayashi | |
age: 11 name: Sara Kimoto | |
age: 12 name: Toru Watanabe | |
age: 17 name: Miss Saeki | |
age: 26 name: Haruki Oumi | |
age: 26 name: Kafka Tamura | |
age: 26 name: Satoru Nakata | |
age: 26 name: Tengo Kawana | |
age: 26 name: Yoshio Oumi | |
age: 26 name: Yuzuki Shirane | |
age: 32 name: Kei Akamatsu | |
age: 32 name: Tsukuru Tazaki | |
age: 42 name: Fumiaki Haida | |
age: 54 name: Eri Kurono | |
*/ | |
/* name as object */ | |
var nestedNames = [ | |
{ name: { first: 'Kei', last: 'Akamatsu'}, | |
age: 32 }, | |
{ name: { first: 'Fumiaki', last: 'Haida'}, | |
age: 42 }, | |
{ name: { first: 'Tengo', last: 'Kawana'}, | |
age: 26 }, | |
{ name: { first: 'Sara', last: 'Kimoto'}, | |
age: 11 }, | |
{ name: { first: 'Midori', last: 'Kobayashi'}, | |
age: 11 }, | |
{ name: { first: 'Eri', last: 'Kurono'}, | |
age: 54 }, | |
{ name: { first: 'Haruki', last: 'Murakami'}, | |
age: 6 }, | |
{ name: { first: 'Satoru', last: 'Nakata'}, | |
age: 26 }, | |
{ name: { first: 'Yoshio', last: 'Oumi'}, | |
age: 26 }, | |
{ name: { first: 'Haruki', last: 'Oumi'}, | |
age: 26 }, | |
{ name: { first: 'Miss', last: 'Saeki'}, | |
age: 17 }, | |
{ name: { first: 'Yuzuki', last: 'Shirane'}, | |
age: 26 }, | |
{ name: { first: 'Kafka', last: 'Tamura'}, | |
age: 26 }, | |
{ name: { first: 'Tsukuru', last: 'Tazaki'}, | |
age: 32 }, | |
{ name: { first: 'Toru', last: 'Watanabe'}, | |
age: 12 } | |
]; | |
console.log( | |
nestedNames | |
.slice() | |
.sort(byAgeThenName) | |
.map(function(item) { | |
return ''.concat('age: ', item.age, ' name: ', item.name.last, ', ', item.name.first) | |
}) | |
.join('\n') | |
); | |
// should see: | |
/* | |
age: 6 name: Murakami, Haruki | |
age: 11 name: Kimoto, Sara | |
age: 11 name: Kobayashi, Midori | |
age: 12 name: Watanabe, Toru | |
age: 17 name: Saeki, Miss | |
age: 26 name: Kawana, Tengo | |
age: 26 name: Nakata, Satoru | |
age: 26 name: Oumi, Haruki | |
age: 26 name: Oumi, Yoshio | |
age: 26 name: Shirane, Yuzuki | |
age: 26 name: Tamura, Kafka | |
age: 32 name: Akamatsu, Kei | |
age: 32 name: Tazaki, Tsukuru | |
age: 42 name: Haida, Fumiaki | |
age: 54 name: Kurono, Eri | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment