title: Dynamic orderBy in AngularJS
body: <p>I have an array of objects I want to order dynamically, based on a value from a drop down menu. This is what I have so far in my list:</p>
  
  <pre><code>ng-repeat="item in filteredItems = (items | filter:searchInput | orderBy:canBeAnything)"
  </code></pre>
  
  <p>But the problem however is that the sorting can be an attribute of the object or a calculated value using a function. It also should be able to sort in a descending way (optionally).</p>
  
  <p>I know I can use a string for the canByAnything variable behind the orderBy passing an attribute of the object like:</p>
  
  <pre><code>“-creationDate” // descending ordering on creation date
  “customer.lastname” // ascending ordering on customers last name
  </code></pre>
  
  <p>I also know I can orderBy a function like:</p>
  
  <pre><code>orderBy:myCalculatedValueFunction // will order in an ascending way based on a calculated value, for example calculating the total price of the object if it was an order or something
  </code></pre>
  
  <p>But what I don't know and want to achieve is:</p>
  
  <ul>
  <li>How to combine it so I can use function(s) for sorting in combination with attributes/properties of the objects. I mean one or the other, dynamically, based on what the user has selected. Which can be an attribute or a calculated value, either descending or ascending.</li>
  <li>How to sort a calculated value in a descending way</li>
  </ul>