Reputation: 430
I am not able to order a key value pair with respect to value
in alphabetical order.
The sample which I am using is,
"week_days_short":[
{"key":"1","value":"Mon"},
{"key":"2","value":"Tue"},
{"key":"4","value":"Thu"},
{"key":"3","value":"Wed"},
{"key":"5","value":"Fri"},
{"key":"6","value":"Sat"},
{"key":"7","value":"Sun"}
]
I have used the following code,
ng-options="key as value for (key, value) in
properties.week_days_short |
orderBy:'value' track by value"
But the sorting not happening. Any suggestions?
Upvotes: 0
Views: 627
Reputation: 430
As orderBy in HTML won't work on Object and week_days_short is array. So sorting on controller fixed my issue.
Object.values($scope.properties.week_days_short).sort()
Upvotes: 0
Reputation: 48968
angular.module("app",[])
.controller("ctrl", function($scope) {
$scope.properties = {
"week_days_short":[
{"key":"1","value":"Mon"},
{"key":"2","value":"Tue"},
{"key":"4","value":"Thu"},
{"key":"3","value":"Wed"},
{"key":"5","value":"Fri"},
{"key":"6","value":"Sat"},
{"key":"7","value":"Sun"}
]};
})
<script src="//unpkg.com/angular/angular.js"></script>
<body ng-app="app" ng-controller="ctrl">
selection={{selection}}<br>
Select with Alphabetical Order<br>
<select ng-model="selection"
ng-options="item.key as item.value for item in
properties.week_days_short |
orderBy:'value' track by item.value">
<option value="">Select day</option>
</select>
</body>
For more information, see
Upvotes: 1
Reputation: 1
I think it can help you.
ng-options="key as value.sort((a, b) => a.key.localeCompare(b.key)) for (key, value) in
properties.week_days_short |
orderBy:'value' track by value"
Here is Ref: In ES6/ES2015 or later you can do this way
Upvotes: -2