json object list sort using id column in angular js

  angularjs, javascript

I need to sort my json data object before render the browser. this is the my json format

$scope.cityData= [
    {
        id: '520',
        city:'col01'
    },
    {
        id: '410',
        city:'col02'
    },
    {
        id: '412',
       city:'col03'
    }]

I tried to do it. but something wrong in my code. I console log it. but did not sort

$scope.cityData.sort(function (a, b) { return a.id - b.id });
console.log($scope.cityData); 
$scope.newCitySort = $scope.cityData;

I need to assign this sorted data to new scope variable also. how i do this ?

Source: Ask Javascript Questions

LEAVE A COMMENT