如何使用AngularJs或Javascript对数组进行排序?

时间:2022-05-30 15:57:28

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.

我想按字母顺序排序和显示数组一旦用户选择或者当我们从后端渲染数据时我想按字母顺序显示全名。美元的范围。selecedControlOwner是ng-click事件处理器一旦用户从模态窗口中选择所有者并单击Ok ng-click事件触发器并在父窗口中显示值现在我想要触发排序。

$scope.controlOwnerObj.workerName is ng-model that is binding the values to parent window.

scope.controlOwnerObj美元。workerName是ng-model,它将值绑定到父窗口。

Is there any solution using AngularJs or native Javascript ?

是否有使用AngularJs或本机Javascript的解决方案?

ctrl.js

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",
        }],

3 个解决方案

#1


2  

use javascript built-in sort function

使用javascript内置的排序函数

$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);
});

#2


0  

I will be using only pure javascript, since you gave us that as an option

我将只使用纯javascript,因为您给了我们一个选项

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]

结果:[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]

结果[213、121、44、12、12、3]

#3


0  

Hi you can use angularjs orderby..

你好,你可以用angularjs orderby。

https://docs.angularjs.org/api/ng/filter/orderBy

https://docs.angularjs.org/api/ng/filter/orderBy

#1


2  

use javascript built-in sort function

使用javascript内置的排序函数

$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);
});

#2


0  

I will be using only pure javascript, since you gave us that as an option

我将只使用纯javascript,因为您给了我们一个选项

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]

结果:[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]

结果[213、121、44、12、12、3]

#3


0  

Hi you can use angularjs orderby..

你好,你可以用angularjs orderby。

https://docs.angularjs.org/api/ng/filter/orderBy

https://docs.angularjs.org/api/ng/filter/orderBy