Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If <code>$scope.items</code> were an array of objects (as implied by <code>$scope.items = []</code>), you would be able to use the angular <code>orderBy</code> <a href="http://docs.angularjs.org/api/ng.filter%3aorderBy" rel="nofollow">filter</a>:</p> <pre><code>&lt;!-- This example will NOT work with Firebase --&gt; &lt;div ng-repeat="item in items | orderBy:'title'"&gt; &lt;span&gt;{{item.title}}&lt;/span&gt; &lt;/div&gt; </code></pre> <p><strong>However</strong>, setting <code>items</code> to an array as you did is misleading, because you are actually storing (and retrieving) your "items" as a list of key value pairs, <strong>not</strong> an array. Sorting objects in javascript is more complicated in general, but one way to achieve what you want is a custom filter:</p> <pre><code>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] &lt; b[attribute]) return -1; if (a[attribute] &gt; b[attribute]) return 1; return 0; } array.sort(compare); return array; } }); </code></pre> <p>Now you can use your filter to order by any of your <code>item</code> properties:</p> <pre><code>&lt;div ng-repeat="item in items | orderObjectBy:'title'"&gt; &lt;span&gt;{{item.title}}&lt;/span&gt; &lt;/div&gt; </code></pre> <p>Here is a <a href="http://jsfiddle.net/hiattp/Bnvnn/3/" rel="nofollow">fiddle</a> 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 <a href="https://github.com/angular/angular.js/issues/1286#issuecomment-14303275" rel="nofollow">here</a>.</p> <p>And just to be thorough I should note that you could also sort "server-side" with Firebase <a href="https://www.firebase.com/docs/ordered-data.html" rel="nofollow">priorities</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload