dates not properly sorted in Angular.js

前端 未结 3 1885
轮回少年
轮回少年 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:10

    If you want to just order Ascending or Descending, simply

    <tr ng-repeat="record in data | orderBy:record.creation_date">
        <td>{{record.name}}</td>
    </tr>
    
    0 讨论(0)
  • 2021-01-04 20:15

    just you need to always keep your date in similar format.

    change

    data = [
      {
        "name": "Test_1_Test",
        "creation_date": "08/02/2013 10:31:02 AM"
      },
      {
        "name": "Test_2_Test",
        "creation_date": "08/01/2013 9:12:32 AM"
      },
      {
        "name": "Test_3_Test",
        "creation_date": "09/13/2013 4:55:09 AM"
      }
    ]
    

    Check fiddle updated

    http://jsfiddle.net/HrGdt/28/

    0 讨论(0)
  • 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
    };
    
    0 讨论(0)
提交回复
热议问题