Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

orderBy filter doesn't support sorting by the value itself #4579

Closed
georgiosd opened this issue Oct 22, 2013 · 8 comments
Closed

orderBy filter doesn't support sorting by the value itself #4579

georgiosd opened this issue Oct 22, 2013 · 8 comments

Comments

@georgiosd
Copy link

For example, it's impossible to sort an array of strings without creating an identity function

Happy to make the mod and send a PR if you will merge it.

@wesleycho
Copy link
Contributor

Here is a plnkr reproducing it: http://plnkr.co/edit/ivpyUNZvaC6SlJAbrkzH?p=preview

It looks like orderBy does nothing currently

@georgiosd
Copy link
Author

Yeah I don't think that item would be accessible like that. I looked at the source and if it can't find a predicate it will just return the array unsorted. IMO that should default to the item itself being the predicate.

If that would be considered as breaking backwards compatibility (though I imagine no one actually uses it this way as it does nothing), an 'identity' option could be considered.

@wesleycho
Copy link
Contributor

But couldn't you conceivably create a conflict if someone has a key named identity in an object in the array?

Is this workaround no good for a particular reason? http://plnkr.co/edit/OAm1GTNRV9tkRMcJJe9E?p=preview

@georgiosd
Copy link
Author

It's possible, though unlikely. Of course there are workarounds - the simplest one being passing _.identity as the predicate. However, it makes sense for this functionality to be in orderBy itself.

I'd say orderBy:'' is the solution with least side effects - I find it veeeery unlikely someone has done that as it's a no-op currently.

@IgorMinar
Copy link
Contributor

@wesleycho's plunk doesn't work because item is available only after the in part of the expression expression is evaluated.

@IgorMinar
Copy link
Contributor

wouldn't it make sense to change orderBy to consider no parameter as predicate that is an identity function?

so you would do this in the template:

<p ng-repeat="item in items | orderBy">{{item}}</p>

such change should be trivial

@IgorMinar
Copy link
Contributor

unless I'm missing something, I say: "PR PLZ!"

@BeheadedKamikaze
Copy link

+1

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.