4
  $scope.items = [];
  var ref = new Firebase("https://****.firebaseio.com/");
  var query = ref.limit(5);

  angularFire(query, $scope, "items");

<div ng-repeat="item in items" >
<span>{{item.title}}</span>
</div>

How would you sort these by "title"? I've seen a couple similar questions but the solutions don't seem to work, at least for this situation.

0

1 Answer 1

4

If $scope.items were an array of objects (as implied by $scope.items = []), you would be able to use the angular orderBy filter:

<!-- This example will NOT work with Firebase -->
<div ng-repeat="item in items | orderBy:'title'">
  <span>{{item.title}}</span>
</div>

However, setting items to an array as you did is misleading, because you are actually storing (and retrieving) your "items" as a list of key value pairs, not an array. Sorting objects in javascript is more complicated in general, but one way to achieve what you want is a custom filter:

app.filter('orderObjectBy', function(){
  return function(input, attribute) {
    if (!angular.isObject(input)) return input;

    var array = [];
    for(var objectKey in input) {
      array.push(input[objectKey]);
    }

    function compare(a,b) {
      if (a[attribute] < b[attribute])
        return -1;
      if (a[attribute] > b[attribute])
        return 1;
      return 0;
    }

    array.sort(compare);
    return array;
  }
});

Now you can use your filter to order by any of your item properties:

<div ng-repeat="item in items | orderObjectBy:'title'">
  <span>{{item.title}}</span>
</div>

Here is a fiddle to demonstrate. As @bennlich noted below, your custom filter could stop at the array conversion and then you could use the normal orderBy filter, as suggested here.

And just to be thorough I should note that you could also sort "server-side" with Firebase priorities.

8
  • 1
    Nope, that doesn't seem to do anything, I don't even get an error when I give it a nonsense value. Nov 21, 2013 at 0:21
  • Are you storing items as an array in firebase?
    – hiattp
    Nov 21, 2013 at 0:26
  • Yep, using the Firebase PHP library: github.com/ktamas77/firebase-php $story = array( 'title' => $title, 'id' => $id ); $response = $fb->set($storyPath, $story); Nov 21, 2013 at 0:30
  • I'm not familiar with the php library but my guess is you aren't actually storing your stories as an array of story objects with "sortable" attributes. If you could post a screenshot or some representation of the data from your forge it would be easier to tell. If you can't sort it with a normal angular filter you'll have to write your own or sort it with firebase priorities.
    – hiattp
    Nov 21, 2013 at 0:35
  • Ah, I don't think I know how to give them sortable attributes and I don't think Firebase PHP can do setpriority, here's a shot: imgur.com/QZ7tkpQ EDIT: better shot: imgur.com/q4DcRvX Nov 21, 2013 at 0:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.