AngularJS使用空值对列表进行排序 [英] AngularJS sorting a list with nulls

查看:63
本文介绍了AngularJS使用空值对列表进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个作用域对象,该对象的列表包含空值,我想在两个方向对该列表进行排序,但空值必须始终在末尾.通过将对象分成2个(一个带空值)和一个不带空值的对象,我可以很容易地做到这一点.但是,有没有更有效的方法呢?

I have a scope object that has a list containing null values, I would like to sort this list in both directions but the null values must be always at the end. I can probably do this easily by splitting up the object into 2, one with nulls and one without then stick on the null list at the end. But is there a more efficient way to do this?

示例:

[1,null,5,7,2,null]

已排序:

ASD: 1,2,5,7,null,null

DESC: 7,5,2,1,null,null

推荐答案

如果只想对数组进行排序,则JavaScript具有方法array.sort.这需要比较器功能.

If you just want to sort the array, JavaScript has method array.sort. This takes a comparer function.

您可以实现两个这样的功能,一个用于升序排序,另一个用于降序.在这两个函数中,您可以根据需要处理空值.请在此处

You can implement two of such functions one for ascending sorting and one for descending. Within these two functions you can treat nulls as you wish. See documentation here

如果您使用angularjs过滤器 orderby ,它也需要比较器功能,因此类似的方法可能有效.

If you are using angularjs filter orderby it too takes a comparer function, so a similar approach may work.

这篇关于AngularJS使用空值对列表进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆