1

I have dropdown with array of objects for options:

<select class="auc-form-control" ng-options="dataValue.Id as dataValue.Name for (dataKey, dataValue) in Types" ng-model="name">
</select>

Options are sorted by Id like this:

<option value="0">0</option>
<option value="1">1</option>
<option value="10">10</option>
<option value="11">11</option>
...

How can I sort options in a natural way? [1, 2, 3, ...]

2
  • Could you please show what does the Types object look like? Commented Aug 13, 2014 at 16:50
  • 1
    You are lacking information here. As Artyom in his answer and comment already pointed out, [0, 1, 10, 11] is the natural order, i.e. for numbers. From your question it is unclear how you want the objects to be ordered or what you consider "natural". Please clarify. Commented Aug 13, 2014 at 17:05

2 Answers 2

2

It seems like your Id properties are strings (not numbers).

In that case, you have to use a custom getter to get a value for sorting. For example:

$scope.getSortValue = function (item) {
  return +item.Id; // convert string to number
};

Then use it with the orderBy filter like this:

<select class="auc-form-control" ng-options="dataValue.Id as dataValue.Name for (dataKey, dataValue) in Types | orderBy:getSortValue" ng-model="name"></select>

Example Plunker: http://plnkr.co/edit/TfVrfhaiuBKbPpHSqp4Q?p=preview

Sign up to request clarification or add additional context in comments.

Comments

1

What about orderBy filter that can be used like this:

"ng-options="dataValue.Id as dataValue.Name for (dataKey, dataValue) in Types" | orderBy:'Id'"

Please see this JSFiddle with your example.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.