I wanted to sort and display array in alphabetical order once user make selection or when we render data from backend i want to display fullName
in alphabetical order. $scope.selecedControlOwner
is ng-click
event handler once user select owners from the modal window and click Ok ng-click
event trigger and display values on parent window Now here i want to trigger sorting.
$scope.controlOwnerObj.workerName
is ng-model that is binding the values to parent window.
Is there any solution using AngularJs or native Javascript ?
ctrl.js
$scope.selectedControlOwner = function() {
$scope.controlOwnerObj.workerName= $scope.selectedOwners.map(function (owner) { return owner.fullName; }).join(';');
};
$scope.selectedOwners = [{
"workerKey": 46958,
"fullName": ,"Kumari, Swapna"
}, {
"workerKey": 746,
"fullName": "Mike Piero",
}, {
"workerKey": 150918,
"fullName": "A J, Jyothish",
}],
I wanted to sort and display array in alphabetical order once user make selection or when we render data from backend i want to display fullName
in alphabetical order. $scope.selecedControlOwner
is ng-click
event handler once user select owners from the modal window and click Ok ng-click
event trigger and display values on parent window Now here i want to trigger sorting.
$scope.controlOwnerObj.workerName
is ng-model that is binding the values to parent window.
Is there any solution using AngularJs or native Javascript ?
ctrl.js
$scope.selectedControlOwner = function() {
$scope.controlOwnerObj.workerName= $scope.selectedOwners.map(function (owner) { return owner.fullName; }).join(';');
};
$scope.selectedOwners = [{
"workerKey": 46958,
"fullName": ,"Kumari, Swapna"
}, {
"workerKey": 746,
"fullName": "Mike Piero",
}, {
"workerKey": 150918,
"fullName": "A J, Jyothish",
}],
Share
Improve this question
asked Apr 12, 2016 at 17:21
hussainhussain
7,13321 gold badges87 silver badges165 bronze badges
3
- stackoverflow./questions/1129216/… – Kalman Commented Apr 12, 2016 at 17:28
- stackoverflow./questions/19259233/… – Hugo S. Mendes Commented Apr 12, 2016 at 17:29
- 1 Possible duplicate of Sorting an array of JavaScript objects – Igor Commented Apr 12, 2016 at 17:30
3 Answers
Reset to default 2use javascript built-in sort function
$scope.selectedOwners = [{
"workerKey": 46958,
"fullName": ,"Kumari, Swapna"
}, {
"workerKey": 746,
"fullName": "Mike Piero",
}, {
"workerKey": 150918,
"fullName": "A J, Jyothish",
}],
$scope.selectedOwners.sort(function(a, b) {
return a.fullName.localeCompare(b.fullName);
});
I will be using only pure javascript, since you gave us that as an option
This sorts them from low to height
var arr = [12, 213, 3, 121, 44, 12];
arr.sort(function (x, y) {
return x > y;
})
It doesn't returns a new array.
Result: [3, 12, 12, 44, 121, 213]
this sorts them from height to low
arr.sort(function (x, y) {
return x < y;
})
Result [213, 121, 44, 12, 12, 3]
Hi you can use angularjs orderby..
https://docs.angularjs/api/ng/filter/orderBy
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745585656a4634524.html
评论列表(0条)