最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin8浏览0评论

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

发布评论

评论列表(0)

  1. 暂无评论