【问题标题】:Custom sort function in AngularJSAngularJS 中的自定义排序功能
【发布时间】:2013-10-26 00:39:43
【问题描述】:

如何制作自定义函数来订购?

我做了这个plunker,并注意到我一次得到一个元素,这使我无法对数组的元素进行排序。有什么想法吗?

我看到了这两个问题,但没有任何帮助: angularjs custom sort function in ng-repeat e AngularJS: Custom Sorting Function using OrderBy

【问题讨论】:

    标签: javascript sorting angularjs


    【解决方案1】:

    您需要从函数中返回一个值,该值将与其他值进行比较。

    例如,您希望在 plunkr 中按生日排序。

    您可以使用如下函数来完成此操作:

    $scope.customSortFunc = function(person) {
       return new Date(person.birthday);
    };
    

    请记住,不同的浏览器支持解析不同的日期格式,因此理想情况下您应该使用 momentjs 之类的内容。

    Example Plunkr

    【讨论】:

    • 谢谢@JohnLedbetter。在我使用sugar的情况下
    猜你喜欢
    • 1970-01-01
    • 2013-02-25
    • 1970-01-01
    • 2013-10-22
    • 2012-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多