Skip to content

Instantly share code, notes, and snippets.

@TomckySan
Last active December 1, 2017 08:04
Show Gist options
  • Save TomckySan/944f6b6e6b99df4eac7b to your computer and use it in GitHub Desktop.
Save TomckySan/944f6b6e6b99df4eac7b to your computer and use it in GitHub Desktop.
var arr = [
{
name: 'たなか',
sex: '男',
age: 15
},
{
name: 'さとう',
sex: '女',
age: 20
},
{
name: null,
sex: '男',
age: 30
},
{
name: 'いまむら',
sex: '女',
age: 19
}
]
arr.sort(function(arg1, arg2) {
// nullと比較するとうまくソートできない
return (arg1.name || '') > (arg2.name || '') ? 1 : -1;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment