javascript - Make nulls always last when sorting - Stack Overflow

I am using underscore to sort an array. Is there a way to make nulls always last whether ascending or d

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 }];

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742319595a4421536.html

相关推荐

  • javascript - Make nulls always last when sorting - Stack Overflow

    I am using underscore to sort an array. Is there a way to make nulls always last whether ascending or d

    11小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信