AngularJS ng-repeat how to orderBy for object of objects?

I have a response from the server like this:

0: {
    0: {
        category: "value1",
        date: "date",
        id: 100,
        title: "MyTitle",
        edituser: "user1",
    }
    hashKey: "02G",
    _proto:{
        //not important,
    }
},
1: {
    1: {
        category: "value2",
        date: "date",
        id: 101,
        title: "MyTitle2",
        edituser: "user2",
    }
    hashKey: "02H",
    _proto:{
        //not important,
    }
},

which need to be shown in a list like this:

<tr ng-repeat="news in newsList[currentPage] | orderObjectBy:sortingOrder:reverse">
        <td>{{news[$index].id}}</td>
        <td>{{news[$index].title}}</td>
        <td>{{news[$index].category}}</td>
        <td>{{news[$index].edituser}}</td>
        <td>{{news[$index].date}}</td>
    </tr>

It shows just fine, but the ordering is not fine. Using normal orderBy won't do it, so I created a custom filter like this (in coffeescript):

app.filter "orderObjectBy", ->
(items, field, reverse) ->
    filtered = []
    angular.forEach items, (item) ->
        filtered.push item

    filtered.sort (a, b) ->
        a[field] > b[field]

    filtered.reverse()  if reverse
    filtered

but still it won't sort, is there any solution to sort that kind of response object in angular?

Answers


Mistake is in the compare function. It should return number. See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

This works:

app.filter('orderObjectBy', () ->
    (items, field, reverse) ->
        filtered = []

        for key, item of items
            filtered.push(item)

        filtered.sort( (a, b) ->
            if a[field] > b[field]
                return 1 
            else 
                return -1
    )

        if (reverse) 
            filtered.reverse()

        return filtered

); 

Need Your Help

Cakephp ADmad CakePHP-HybridAuth Plugin threw Exception

php cakephp-3.0 hybridauth

I am using ADmad/CakePHP-HybridAuth Plugin for Social auth. For this i followed the doc that given at https://github.com/ADmad/CakePHP-HybridAuth. I did same as it that done on doc but i am facing an