I am trying to implement a ng-repeat functionality into the divs. For that I need to sort the data first (according to the rank it has, provided from an object) and then name it into the div. So I am creating a custom OrderBy function . My sorting functionality works well. However, I am always getting the order of the divs as the same. (Infact, exactly opposite of what I need). I tried using reverse functionalities, but it doesn't work.
My sorting code looks like this:
var res = {
"Toyota": 2,
"Ford": 1,
"Chrysler": 4,
"Hyundai": 3,
"Nissan": 5
};
sortedVal = Object.keys(res).sort(function (a, b){
return res[a] - res[b];
});
My HTML div looks like this,
<div class="card" ng-repeat="name in data | orderBy: sortedVal ">
I get the correct output in the console. However, the divs are always arranged in the order given below, no matter what I do.
Nissan, Chrysler, Hyundai, Toyota, Ford.
I need them in the order: Ford, Toyota, Hyundai, Chrysler, Nissan.
Note: Don't worry about the "name in data" part, it works well. I am having problems with "OrderBy".
You have to add
sortedValto the controller scope and then it will be available in the view.Or you can create a custom filter to order the objects by value. Something like this.
Html
Js
Demo http://plnkr.co/edit/jdIf9hjiUrR6la02aVjA?p=preview