문제

I'm trying to sort two columns with angular js, column average and another time in seconds, but the seconds not sorted correctly. I currently returns this:

enter image description here

I'm doing something as a ranking.

my html:

<tr ng-repeat="user in ranking | orderBy:['-promedio','tiempo']" >
  <td >{{ user.usuario }}</td>
  <td > {{ user.promedio }} </td>
  <td > {{ user.tiempo}} </td>
</tr>

my ctrl:

EncuestApp.controller('ResultadoCtrl', function ($scope) { $scope.ranking =[ { "id":"5", "tiempo":"15.737", "promedio":"0.4", "usuario":"rfmartinez" }, { "id":"6", "tiempo":"35.694", "promedio":"0.8", "usuario":"mm" }, { "id":"7", "tiempo":"303.328", "promedio":"0.8", "usuario":"mn" }, { "id":"8", "tiempo":"40.327", "promedio":"0.8", "usuario":"mnn" } ]; });

which is the best way to solve it?

thanks

도움이 되었습니까?

해결책

You have stored all numbers as strings (e.g. "10.5" instead of just 10.5). Therefore AngularJS sorts your entries by the textual representation of the numbers. Change all numbers to actual numbers in your data model:

EncuestApp.controller('ResultadoCtrl', function ($scope) {
    $scope.ranking =[
        {
        "id": 5,
        "tiempo": 15.737,
        "promedio": 0.4,
        "usuario": "rfmartinez"
        },
        {
        "id": 6,
        "tiempo": 35.694,
        "promedio": 0.8,
        "usuario": "mm"
        },
        {
        "id": 7,
        "tiempo": 303.328,
        "promedio": 0.8,
        "usuario": "mn"
        },
        {
        "id": 8,
        "tiempo": 40.327,
        "promedio": 0.8,
        "usuario": "mnn"
        }
    ];
});
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top