1

我想按日期排序我的数据,但有些日期值是空的。我将如何对数据进行排序并将空值放在列表的末尾以进行降序,而将空值放在列表的开头以进行升序?

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 项如何分隔日期,我不确定为什么会发生这种情况。任何人都可以帮忙吗?

编辑:该解决方案不能有任何硬编码,因为我将在单击标题标题时动态更改要排序的列。无论数据类型如何,空值问题都应该起作用。只要有空值,我就需要将它们组合在一起。

谢谢!

4

1 回答 1

3

只需为 null 的情况添加一个占位符值。由于您是按降序排序并且最终需要它,因此请给它们尽可能小的值。您可能想在之后添加一个 thenby 子句。

var enu = $.Enumerable.From(data)
    .OrderByDescending("$.v || '0000'")
    .ThenBy("$.n")
    .ToArray();

小提琴

于 2015-06-22T16:05:43.153 回答