最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Make nulls always last when sorting - Stack Overflow

programmeradmin0浏览0评论

I am using underscore to sort an array. Is there a way to make nulls always last whether ascending or descending? For example:

[
    { name: 'a', age: 1 }, 
    { name: 'z', age: 5 }, 
    { name: '', age: 1 }, 
    { name: 'v', age: 7 }
]

Will produce

[
    { name: 'a', age: 1 }, 
    { name: 'v', age: 7 }, 
    { name: 'z', age: 5 }, 
    { name: '', age: 1 }
]

and descending will produce:

[
    { name: 'z', age: 5 }, 
    { name: 'v', age: 7 }, 
    { name: 'a', age: 1 }, 
    { name: '', age: 1 }
]

My real array is an array of objects so i have to pluck values out.

I am using underscore to sort an array. Is there a way to make nulls always last whether ascending or descending? For example:

[
    { name: 'a', age: 1 }, 
    { name: 'z', age: 5 }, 
    { name: '', age: 1 }, 
    { name: 'v', age: 7 }
]

Will produce

[
    { name: 'a', age: 1 }, 
    { name: 'v', age: 7 }, 
    { name: 'z', age: 5 }, 
    { name: '', age: 1 }
]

and descending will produce:

[
    { name: 'z', age: 5 }, 
    { name: 'v', age: 7 }, 
    { name: 'a', age: 1 }, 
    { name: '', age: 1 }
]

My real array is an array of objects so i have to pluck values out.

Share Improve this question edited Nov 24, 2014 at 8:21 Evgeniy 2,9213 gold badges22 silver badges35 bronze badges asked Nov 23, 2014 at 20:15 Luke101Luke101 65.3k88 gold badges241 silver badges374 bronze badges 11
  • You can remove all the null values, why put them on the end? – Bazinga Commented Nov 23, 2014 at 20:18
  • I am trying to sort a property on an object. So even if a property is null the other properties are useful. – Luke101 Commented Nov 23, 2014 at 20:22
  • You can make a plunker so i can try to help you? – Bazinga Commented Nov 23, 2014 at 20:24
  • Why do you need to do this? – istos Commented Nov 23, 2014 at 20:25
  • I am trying to mimick a database "order by Name NULLS LAST" – Luke101 Commented Nov 23, 2014 at 20:27
 |  Show 6 more ments

3 Answers 3

Reset to default 6

There are many ways to go about it but you could try something like this:

var arr = [{name: 'a', age: 1}, {name: 'z', age: 5}, {name: '', age: 1}, {name: 'v', age: 7 }];

_.chain(arr)
 .sortBy('name')
 //.reverse() // to sort descending
 .partition('name')
 .flatten()
 .value();

Result:

[ { name: 'a', age: 1 },
  { name: 'v', age: 7 },
  { name: 'z', age: 5 },
  { name: '', age: 1 } ]

You could simply find them after sorting and put them in the back manually. Maybe by splitting the different values in 2 arrays and then concatenating them. One of the arrays contains non null values and the other the null values

You can do this with lodash, and im think underscore as this function too.

var users =[{name: 'a', age: 1}, {name: '', age: 4}, {name: 'z', age: 5}, {name: '', age: 1}, {name: 'v', age: 7 }];

_.sortBy(users, 'name');

The results will give you the null names in the beginning:

[{name: '', age: 1}, {name: '', age: 4}, {name: 'a', age: 1}, {name: 'z', age: 5}, {name: 'v', age: 7 }];
发布评论

评论列表(0)

  1. 暂无评论