The best thing to do is make sure that the data come off the back end
properly if possible. The JSON spec allows for numbers (unquoted), for
example.

The next best thing may be to wrap the json you do receive in some
encapsulating code. You could define some kind of clever sortBy method that
introspects and decides if parseInt returns NaN or not. Or you could stick
the parser right after the $http.get, so that your service munges the data
to ensure all numbers are actually numbers.

That last bit is something I used to do, as I had some extended data stored
in an HSTORE column in my postgres database, which stores strings. So I
define all my fields ahead of time on the angular side and type them, to
coerce them properly when they come in. I've since switched to a JSON
column type for that (extended stuff like experiments we may not keep or
fields a small number of records have which don't need indexing), which
does come off typed, so that's cool.

E

On Fri, Nov 28, 2014, 6:20 AM Leonard Modoran <[email protected]> wrote:

> Thank you Eric for the answer.
>
> The problem is that all the fields will be changed in the future, and I
> could not hard-code a field so it will be interpreted as a number, because
> tomorrow that filed could be a text.
>
> My question was more if it's possible, and how, if you don't know the
> fields, to interpret the expression, and understand that is a number, and
> make angular know that is a number
>
>
> In the mean time I've put the values from the db to come in a text format,
> and not string... so the problem is solved :)
>
> Thank you!
>
>
> On Thursday, November 27, 2014 5:17:33 PM UTC, Eric Eslinger wrote:
>
>> You can write your own sort getter. Instead of orderBy:'age', you can put
>> a function on the scope that does a better job of getting the search key.
>>
>> Ex:
>> $scope.sorter = function(val) {return parseInt(val.age)}
>>
>> ng-repeat="item in theList | orderBy:sorter"
>>
>> e
>>
>>
>> On Thu Nov 27 2014 at 8:11:12 AM Leonard Modoran <[email protected]>
>> wrote:
>>
> I have a big json object, that have all the key, values as strings so they
>>> are inside the quotes.
>>>
>>> ex. { "Name": "John Doe", "Age": "30"}
>>>
>>> The problems is if you have multiple fields like this, is that you make
>>> a sorting with orderBy on the ng-repeat, the default sorting is by name,
>>> and it's working the same also for numbers.
>>>
>>> so the order will be 1, 10, 11, 2, 20 ,25,3, 40   instead of 1, 2, 3,
>>> 10, 11, 20, 25,40
>>>
>>>
>>> How to convert it so will see it as a number, and this by not altering
>>> the json values.
>>>
>>>
>>> Thank you!
>>>
>>> --
>>> 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 [email protected].
>>> To post to this group, send email to [email protected].
>>
>>
>>> Visit this group at http://groups.google.com/group/angular.
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>  --
> 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 [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/angular.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to