我想按日期排序我的数据,但有些日期值是空的。我将如何对数据进行排序并将空值放在列表的末尾以进行降序,而将空值放在列表的开头以进行升序?
Javascript:
var data = [
{"n": "Adams","v": null},
{"n": "Anderson","v": "2015-05-30T15:07:00.0000000"},
{"n": "Armstrong","v": "2015-06-06T02:09:00.0000000"},
{"n": "Cooper","v": "2015-06-06T02:12:00.0000000"},
{"n": "House","v": "2015-06-12T12:33:00.0000000"},
{"n": "Lee","v": "2015-06-06T00:59:00.0000000"},
{"n": "Marmol","v": "2015-06-06T23:22:00.0000000"},
{"n": "Maronde","v": "2015-06-06T16:46:00.0000000"},
{"n": "Molleken","v": "2015-06-06T16:51:00.0000000"},
{"n": "Murata","v": "2015-06-06T23:20:00.0000000"},
{"n": "Price","v": null},
{"n": "Roth","v": "2015-06-07T14:00:00.0000000"},
{"n": "Soto","v": "2015-06-06T23:46:00.0000000"}
];
var enu = $.Enumerable.From(data)
.OrderBy('$.v')
.Select()
.ToArray();
var vue = new Vue({
el: 'ul',
data: {
title: 'test',
test: enu
}
});
HTML:
<ul>
<li v-repeat="test">{{v}}</li>
</ul>
输出:
null
2015-05-30T15:07:00.0000000
2015-06-06T00:59:00.0000000
2015-06-06T02:09:00.0000000
2015-06-06T02:12:00.0000000
2015-06-06T16:46:00.0000000
2015-06-06T16:51:00.0000000
2015-06-06T23:20:00.0000000
2015-06-06T23:22:00.0000000
2015-06-06T23:46:00.0000000
2015-06-12T12:33:00.0000000
null
2015-06-07T14:00:00.0000000
看我的小提琴:https ://jsfiddle.net/6pcmq89h/
您可以看到 null 项如何分隔日期,我不确定为什么会发生这种情况。任何人都可以帮忙吗?
编辑:该解决方案不能有任何硬编码,因为我将在单击标题标题时动态更改要排序的列。无论数据类型如何,空值问题都应该起作用。只要有空值,我就需要将它们组合在一起。
谢谢!