Angular order Object by properties

I have an angular app which consumes json from an API endpoint. The json data arrives like so:


$scope.tasks = [ {id:23, title:'foo'}, {id:448, title:'bar'} ]

But for the sake of easily accessing the data within angular I map th objects so that I get something like:

$scope.tasks = { 23:{id: 23, title:'foo'}, 448:{id:23, title:'foo'} }

This allows me to pull out task instances and modify task instances using their id only.

The consequence of this that I have lost the easy orderBy methods of Angular, though ng-repeat continues to function as expected.

So, my question is, if I want to now order my list by title or -title, how can this be achieved? Is there anything in Angular to accomplish this or should I roll my own filter?


so in my template I want to do this:

<div ng-repeat="task in tasks | orderBy:'-title'">
    <span>{{ task.title }}</span>

So the orderBy filter works if the code is in the first format but doesn't work if the code is mapped as in the second format.


Why not just write a method to pull out a task based on a given ID, rather than re-format the response from the API and write your own repeater directives?

function getTask(id) {
    for (var i = 0; i < $scope.tasks.length; i++) {
        if ($scope.tasks[i].id == id)
            return $scope.tasks[i];

The above task will still be binded to that $scope.tasks array, so any property changes you make should be reflected in the view.

Need Your Help

Future plans to consider for mvc 5.2 web application, with releasing mvc6 (vnext)

I need to get any ideas of how i should be managing our previous MVC 5.2 web application.. now i am using mvc-5.2 with entity framework 6.0. and i want to start phase-2 for some of ...

FileNotFoundException at HttpURLConnection.getInputStream

java android

I'm getting this error for the following code, The error cause of the openStream() I believe, I just want to get the content of the following URL page , the code works great if I change the URL of ...