3

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",
        }],
hussain
  • 6,587
  • 18
  • 79
  • 152
  • http://stackoverflow.com/questions/1129216/sort-array-of-objects-by-string-property-value-in-javascript – Kalman Apr 12 '16 at 17:28
  • http://stackoverflow.com/questions/19259233/sorting-json-by-specific-element-alphabetically – Hugo S. Mendes Apr 12 '16 at 17:29
  • 1
    Possible duplicate of [Sorting an array of JavaScript objects](http://stackoverflow.com/questions/979256/sorting-an-array-of-javascript-objects) – Igor Apr 12 '16 at 17:30

3 Answers3

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);
});
Farnabaz
  • 4,030
  • 1
  • 22
  • 42
  • Problem here is even array sorted i am assigning `$scope.controlOwnerObj.workerName` to the ng-model , do we need to something with that property little confuse here – hussain Apr 12 '16 at 17:35
  • @hussain if you want to have sorted string of owners name you must sort your array before `map`, like this `.sort(function(a, b) {return a.fullName.localeCompare(b.fullName);}).map(function (owner) { return owner.fullName; }).join(';');` – Farnabaz Apr 12 '16 at 17:43
1

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]

Все Едно
  • 746
  • 3
  • 10
  • 24
1

Hi you can use angularjs orderby..

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

user2811978
  • 13
  • 1
  • 4