0

I'm using AngularJS filter to filter an array of objects inside a controller. These objects all have a quality that is a string. I want to be able to filter the array from largest to smallest. But I can't get it filter on value of the integer inside the string.

array = [{
  object1 {
    value = "3"
  }, {
 object2 {
    value = "1"
  }, {
  object3 {
    value = "22"
  }]

 $filter('orderBy')( array, 'value', true );

This would order the array as: object2, object3, object1. I've tried using parseInt but that doesn't work. Does anyone know the correct syntax?

Mischa
  • 2,069
  • 4
  • 23
  • 32
  • This more a question of how to order a array of objects in javascript rather than a angularjs specific problem. Maybe you take a look at this [example](http://stackoverflow.com/questions/1129216/sort-array-of-objects-by-string-property-value-in-javascript) It may could solve your problem. – ckruczek Jun 18 '15 at 08:52

1 Answers1

1

Replace value with a function that returns the length of the string:

$filter('orderBy')( array, function(item) { return parseInt( item.value ); } , true );
Joao Leal
  • 5,533
  • 1
  • 13
  • 23
  • Almost correct, it turns out to be: { return parseInt( item.value ); }. I'll mark your answer as correct but you might wanna edit your post. Thanks alot though. – Mischa Jun 18 '15 at 09:11
  • Edited! I think the original answer was just a reflection of what I'm doing at work :) – Joao Leal Jun 18 '15 at 09:13