Title: Angular orderby with JSON-hyphen
Tags: javascript;json;angularjs;angularjs-ng-click
Question: I have a JSON-file with hyphen in one attribute. I want to sort JSON-data with that attribute using ng-click.
Here is my code: http://codepen.io/anon/pen/zxooMv
<a href="#" ng-click="orderByField='square-miles'; reverseSort = !reverseSort"&gt;
does not work and neither does
<a href="#" ng-click="orderByField=['square-miles']; reverseSort = !reverseSort"&gt;
What's the syntax i should use in order for it to work?
Thanks.

Here is the accepted answer: orderBy uses the $parse service internally which will parse your property as the expression:
square - miles
To get around this you can instead pass your own getter function like this:
<tr ng-repeat="state in data.states.state|orderBy:orderBy(orderByField):reverseSort"&gt;
And in your controller:
$scope.orderBy = function(property) {
return function(item) {
return item[property];
Demo: http://codepen.io/anon/pen/RNoZLw
Comment for this answer: That is an unrelated issue. They are sorted in that order since the values of square-miles are strings. I assume you were expecting a natural sort order: blog.codinghorror.com/sorting-for-humans-natural-sort-order
Comment for this answer: If you set one of the populations to `618.803.1406` and use the default orderBy you will notice the same behavior (it will not be on top): http://codepen.io/anon/pen/JobOEJ
Comment for this answer: Here is an example that turns them into numbers instead (which will result in a natural sort order): http://codepen.io/anon/pen/ByQmpM
Comment for this answer: Your suggestion works like a charm on your demo. However when i added some more JSON-data it appears to sort data based on square-miles randomly..
Have a look
http://codepen.io/anon/pen/zxooMv
Comment for this answer: Ah yes, natural sort order, that's what i was looking for! Thank you very much!


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2015:00:01/03/15', 'FavoriteCount': 0.0, 'PostTypeId': 1, 'Tags': '<javascript><json><angularjs><angularjs-ng-click>', 'Title': 'Angular orderby with JSON-hyphen', 'ViewCount': 361, 'id': 27756763, 'user_AboutMe': None, 'user_AccountId': 5565572.0, 'user_DisplayName': 'AvoQ', 'user_Reputation': 97.0, 'user_Views': 18.0, 'user_id': 4413016.0}}