How to sort matrix in AngularJS, I have tried this but it not working:
script.js
function MyCtrl($scope){
$scope.matrix = [
[3, 3, 3],
[4, 4, 4],
[2, 2, 2]
];
function sort(){
for (var i = 0; i < $scope.matrix.lenght; i++){
for (var j = 0; i < $scope.matrix.lenght - i; j++){
if($scope.matrix[i][0] > $scope.matrix[j][0]){
var temp = [];
temp = $scope.matrix[i];
$scope.matrix[i] = $scope.matrix[j];
$scope.matrix[j] = temp;
}
}
}
}
And my .html file
<div ng-app>
<div ng-controller="MyCtrl">
<ul ng-init="sort()">
<li ng-repeat="line in matrix">
{{line}}
</li>
</ul>
</div>
</div>
I don`t understand what I am doing wrong
sortfunction is not bounded to scope. Write it as$scope.sort = function(){...}, you can initialise it by calling$scope.sort()in your controller (withoutng-init)