Problem orderBy with numbers

8,120 views
Skip to first unread message

Alan Löffler

unread,
Feb 21, 2013, 2:17:26 PM2/21/13
to ang...@googlegroups.com
I have a table with some data tacken from a mysql database, it looks like this -->

<table class="table table-condensed table-hover">
<thead>
<tr>
<th><a ng-click="predicate='counter';reverse=!reverse">#</a></th>
<th><a ng-click="predicate='product';reverse=!reverse">Producto</a></th>
<th><a ng-click="predicate='title';reverse=!reverse">Título</a></th>
<th><a ng-click="predicate='city';reverse=!reverse">Ciudad</a></th>
<th><a ng-click="predicate='state';reverse=!reverse">Provincia</a></th>
<th><a ng-click="predicate='value';reverse=!reverse">Valor</a></th>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in items | orderBy:predicate:reverse | filter:search | range:rangeModel" style="cursor: pointer;">
<td>{{item.counter}}</td>
<td>{{item.product}}</td>
<td>{{item.title}}</td>
<td>{{item.city}}</td>
<td>{{item.state}}</td>
<td>{{item.value | number}}</td>
</tr>
</tbody>
</table>

The problem is the order of the last row (called Valor), where I have a number data.

Ascending order:
15,000
16,000
1,800
2,000
3,000

Descending order:
3,000
2,000
1,800
16,000
15,000

What I'm doing wrong? I can't figure it out.
Thanks for the help!

Peter Bacon Darwin

unread,
Feb 21, 2013, 2:26:45 PM2/21/13
to ang...@googlegroups.com
You are sorting alphabetically since the values are strings.


--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular?hl=en-US.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Alan Löffler

unread,
Feb 21, 2013, 2:34:28 PM2/21/13
to ang...@googlegroups.com
How can I sort that row as a number?

Alan Löffler

unread,
Feb 21, 2013, 2:48:31 PM2/21/13
to ang...@googlegroups.com
I tried erasing the formatting in the expression {{item.value | number}} to {{item.value}} and using number_format into the php code wich create the json structure, and the sort is not working.
I moved a project to angularjs and this is the only issue to fix. Thanks for the reply Peter.

Peter Bacon Darwin

unread,
Feb 21, 2013, 2:57:34 PM2/21/13
to ang...@googlegroups.com
I think you are probably receiving the number as a string, so try running parseInt(...) on each value before binding them to the scope.


--

Alan Löffler

unread,
Feb 21, 2013, 4:10:08 PM2/21/13
to ang...@googlegroups.com
You were right, It was a format issue. I solved formatting on the PHP side with doubleval() function.
Thanks for the tip.

liz...@gmail.com

unread,
Nov 20, 2015, 6:38:11 PM11/20/15
to AngularJS, alanmati...@gmail.com
please refer the link below which explains how to sort float values using AngularJS

Reply all
Reply to author
Forward
0 new messages