Linq.JS OrderBy具有空值

时间:2015-06-22 14:36:06

标签: javascript vue.js linq.js

我想按日期订购我的数据,但有些日期值为空。我如何排序数据并将空值一起放在我的列表末尾以便下降和我的列表开头升序?

使用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值的问题都应该起作用。只要存在空值,我就需要将它们组合在一起。

谢谢!

1 个答案:

答案 0 :(得分:3)

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

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

fiddle