dates not properly sorted in Angular.js

前端 未结 3 1884
轮回少年
轮回少年 2021-01-04 19:47

I am relatively new to Angular.js and am having a problem with date sorting. I have looked around the web for similar issues, but haven\'t found any that are of quite the sa

3条回答
  •  囚心锁ツ
    2021-01-04 20:19

    be aware that it will compare string. It won't compare Dates. You will have to deal with Dates object.

    Here is a talk about this.

    Here is the solution from vojtajina

    Here is part of the solution:

    Main.prototype = {
    
        sort: function(item) {
            if (this.predicate == 'date') {
                return new Date(item.date);
            }
            return item[this.predicate];
        },
    
        sortBy: function(field) {
            if (this.predicate != field) {
                this.predicate = field;
                this.reverse = false;
            } else {
                this.reverse = !this.reverse;
            }
        },
    
        reverse: false
    };
    

提交回复
热议问题