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

javascript - sort array in angularjs - Stack Overflow

programmeradmin0浏览0评论

Im working on sorting on an array using Angular JS using orderBy. But still its not getting sorted on a particular key.

Here is the code

var app = angular.module('sortModule', [])
app.controller('MainController', function($scope,$filter){    
$scope.languages = [ 
        { name: 'English', image: '/images/english.png',key:2 },
        { name: 'Hindi', image: '/images/hindi.png',key:3 },
    { name: 'English', image: '/images/english.png',key:2},
    { name: 'Telugu', image: '/images/telugu.png',key:1 }];        

var newLanguages = []
newLanguages = angular.copy($scope.languages);  
function sortImages() { 
        $scope.languages = []
    $scope.keys = []        
        for(language in newLanguages) {
            $scope.keys.push(newLanguages[language])
    }
    $filter('orderBy')($scope.keys, 'key')
    console.log(JSON.stringify($scope.keys))
}
sortImages();

});

Fiddle

Im planning to see sorting based on "key". telugu should come first, english next and hindi last.

Im working on sorting on an array using Angular JS using orderBy. But still its not getting sorted on a particular key.

Here is the code

var app = angular.module('sortModule', [])
app.controller('MainController', function($scope,$filter){    
$scope.languages = [ 
        { name: 'English', image: '/images/english.png',key:2 },
        { name: 'Hindi', image: '/images/hindi.png',key:3 },
    { name: 'English', image: '/images/english.png',key:2},
    { name: 'Telugu', image: '/images/telugu.png',key:1 }];        

var newLanguages = []
newLanguages = angular.copy($scope.languages);  
function sortImages() { 
        $scope.languages = []
    $scope.keys = []        
        for(language in newLanguages) {
            $scope.keys.push(newLanguages[language])
    }
    $filter('orderBy')($scope.keys, 'key')
    console.log(JSON.stringify($scope.keys))
}
sortImages();

});

Fiddle

Im planning to see sorting based on "key". telugu should come first, english next and hindi last.

Share Improve this question edited Jan 14, 2016 at 5:14 Subash 7,2668 gold badges47 silver badges72 bronze badges asked Jan 14, 2016 at 5:00 SyedSyed 2,60714 gold badges54 silver badges97 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 15

you need to have:

$scope.keys = $filter('orderBy')($scope.keys, 'key', false) 

the order by filter returns a new array, it does not make changes to the passed array.

updated fiddle: http://jsfiddle.net/kjuemhua/17/

Remove the OrderBy from the html markup to display unordered list:

<div ng-app="sortModule" class="nav">
<div ng-controller="MainController">


    <button ng-click="sort()">Sort  
    </button>
    <div></div>
    <div >        
        <ul>
            <li ng-repeat="lang in languages">
               <span>{{lang.name}}</span>
            </li>
        </ul>
    </div>
    </div>
</div>

Now using the button sort sort the list

var app = angular.module('sortModule', [])
app.controller('MainController', function($scope,$filter){    
    $scope.languages = [ 
            { name: 'English', image: '/images/english.png',key:2 },
            { name: 'Hindi', image: '/images/hindi.png',key:3 },
        { name: 'English', image: '/images/english.png',key:2},
        { name: 'Telugu', image: '/images/telugu.png',key:1 }];        

    var newLanguages = []
    newLanguages = angular.copy($scope.languages);  
    $scope.sort = function(){ 
            $scope.languages = []
        $scope.keys = []        
            for(language in newLanguages) {
                $scope.keys.push(newLanguages[language])
        }
        $scope.keys = $filter('orderBy')($scope.keys, 'key', false);
        $scope.languages = $scope.keys;
        console.log(JSON.stringify($scope.keys))
    }
});
发布评论

评论列表(0)

  1. 暂无评论