nour nour nour nour - 5 months ago 11
AngularJS Question

How to get the json files data in an alphabetic order

I parsed three different json files using AngularJS. Here's my code:

Factory code

app.factory('myapp', ['$http', function($http) {
function getLists() {
var tab = ['url1', 'url2', 'url3'];
var list = [];
for(i = 0; i < tab.length; i++) {
$http.get(tab[i])
.then(function(res) {
list.push(res.data);
});
}
return list;
}

return {
getLists: getLists
};
]);


What I want is to display the data of the different files in a , the data of the first url in the first line , the second one in the second line , etc...
I have to display the data depending on the alphabetic order of the names 'nm'.

Html code:

<tr ng-repeat="d in list">
<td>{{d.nm}}</td>
<td>{{d.cty}}</td>
<td>{{d.hse}}</td>
<td>{{d.yrs}}</td>
</tr>


What should I do?

Answer

you can simply use orderBy

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

like so

<tr ng-repeat="d in list | orderBy: 'nm'">
    <td>{{d.nm}}</td>
    <td>{{d.cty}}</td>   
    <td>{{d.hse}}</td>
    <td>{{d.yrs}}</td> 
</tr>