Vector Order Increasingly with Angularjs

3
$scope.auxiliar.minimo = $scope.blocos[i].tamanhoTotal - processo.tamanho;
$scope.auxiliar.posicaoBlocoLivre = $scope.blocos[i];
$scope.menores.push($scope.auxiliar);

I want to sort my "minor" vector incrementally by object attribute: "$scope.auxiliar.minimo"

    
asked by anonymous 08.05.2016 / 22:59

2 answers

1

Here is a way to organize the array for use in JS by creating a function to be passed as a parameter in sort.

var sortByMinimo = function(a, b) {
    if (a.minimo < b.minimo) {
        return 1;
    }
    if (a.minimo > b.minimo) {
        return -1;
    }
    return 0;
};
$scope.menores.sort(sortByMinimo);

You can use the suggested AngularJs media in the other response, especially if you just want to organize in the view.

    
19.10.2016 / 18:01
0

If your ultimate goal is to show on the screen through the ng-repeat directive you can sort directly during the process this way:

<tr ng-repeat="menor in menores | orderBy:'-minimo'"></tr>

Or through the $ filter

$scope.menores = $filter('orderBy')($scope.menores, '-minimo');

remembering that the + or - prefixed to the property used to sort means ascending or descending respectively.

    
10.05.2016 / 17:08