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