Sort Vector Increasingly with Angularjs

Asked

Viewed 312 times

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 vector "smaller" increasingly by object attribute: "$scope.auxiliar.minimo"

  • See if this helps: https://docs.angularjs.org/api/ng/filter/orderBy

  • Have you tried something like: http://answall.com/questions/100068/order-um-array-objects-por-data/100076#100076

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 means in the other answer, especially if you just want to organize in the view.

0

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

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

Or through the service $filter

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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.