Parthipan S
Parthipan S

Reputation: 180

OrderBy is not working as expected in AngularJS

I want to display the json object in ascending order based on the date. For that I am using OrderBy but is not working as expected, Its working perfectly up to October, but November and December it not working.

HTML page:

<li class="row-fluid" style="height: 65px !important;" ng-repeat="item in insertionListItems | index | orderBy: 'insDate': false" ng-class="{'selected': item.$selected}" ng-click="item.$selected = true; onListItemClicked(item)">
    <div class="col-sm-12 cdm-list-item-content">
        <div>
            <span class="col-sm-6" style="padding-left: 0px">{{item.insDate}}</span>
            <span class="col-sm-6" style="padding-left: 0px">{{item.netTotal |number:2}}</span>
        </div>
    </div>
</li>

Json Structure

    insertionListItems  = [
    {
        "index": 0,
        "insDate": "5-4-2015",
        "insName": "Insertion 1",
        "netTotal": -474299.15
    },
    {
        "index": 1,
        "insDate": "6-10-2015",
        "insName": "Insertion 2",
        "netTotal": 1000
    },
    {
        "index": 2,
        "insDate": "1-22-2015",
        "insName": "Insertion 3",
        "netTotal": 100
    },
    {
        "index": 3,
        "insDate": "12-11-2015",
        "insName": "Insertion 4",
        "netTotal": 500
    }
]

Upvotes: 0

Views: 49

Answers (1)

Shashank Agrawal
Shashank Agrawal

Reputation: 25797

The problem here is that the orderBy filter is ordering a string value like "6-10-2015" but not really a date object. So a normal string comparison is happening which is not at all same as date comparison.

So, if you compare "11-21-2014" and "10-21-2014" as string, it won't give you expected result.

So, you have to parse the date in each item as date object and then only you can apply the orderBy filter.

Upvotes: 1

Related Questions