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

3 Answers 3

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);
});
Sign up to request clarification or add additional context in comments.

2 Comments

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 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(';');
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]

Comments

1

Hi you can use angularjs orderby..

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.