0

I am trying to sort the dictionary in angular JS. Below is my code

 <table ng-if='dedup==false'  class="table table-hover table-striped"  >
        <th>Name</th>
        <th class="value">Count</th>
        <tr ng-repeat="(key,value) in result.Fields[0].FieldDistributions|orderBy:value:reverse ">
            <td>{{key}}</td>
            <td> {{value|number}}</td>
        </tr>
    </table>

I want to sort the table based on the value .

What went wrong in the code. Can any one help me ?

UPDATE :

{"Fields":[{"FieldName":"Employer","FieldDistributions":{"na":"30724","UK Civil Service":"561","National Health Service":"523","Compass Group":"496","Cit Group Limited":"491","Webrecruit Ltd":"266","Med Team Primary Care Services Limited":"135","Johnson & Johnson":"124","The Allstars Group":"95","Pj Carey Contractors Limited":"88"}}]}

This is my response

Thanks,

backtrack
  • 7,996
  • 5
  • 52
  • 99

2 Answers2

1

For the orderBy filter, it's trying to evaluate the property on your scope for a function or string. So, it's not looking at the current value in the list; it's trying $scope.value which is probably undefined.

Rather, it's expecting a string property on each member of your list (or a function to evaluate the sort with). For example, if you had a list of people objects that you wanted to sort by name, you would use orderBy:"name":reverse.

So, you would just supply whatever property exists on your list members that you want to sort by, or implement a function to compare members of your list and pass that to orderBy.

If you're not dealing with objects, but just numeric/string values, then you can do what's suggested in this question: orderBy array item value in Angular ng-repeat.

Docs: https://docs.angularjs.org/api/ng/filter/orderBy

Community
  • 1
  • 1
jfairbank
  • 151
  • 1
  • 5
0

not sure how your modal looks like, but can you give this a try?

<table ng-if='dedup==false'  class="table table-hover table-striped"  >
        <th>Name</th>
        <th class="value">Count</th>
        <tr ng-repeat="rowData in result.Fields[0].FieldDistributions|orderBy:value:reverse ">
            <td>{{rowData.key}}</td>
            <td> {{rowData.value|number}}</td>
        </tr>
    </table>
sagie
  • 1,744
  • 14
  • 15