How can i sort array using AngularJs or Javascript? - Stack Overflow

I wanted to sort and display array in alphabetical order once user make selection or when we render dat

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
Add a ment  | 

3 Answers 3

Reset to default 2

use 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

相关推荐

  • How can i sort array using AngularJs or Javascript? - Stack Overflow

    I wanted to sort and display array in alphabetical order once user make selection or when we render dat

    7小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信