angular.js icon indicating copy to clipboard operation
angular.js copied to clipboard

ngRepeat iterates over object in alphabetical order

Open cztomsik opened this issue 12 years ago • 85 comments

This is very unexpected, all major browsers retain order of inserted keys and it is not going to change in any way in future as it would break a lot of existing code.

No matter of that, angular does it its own "way".

Basically it makes this feature completely useless as you can't use it for example for iterating computed averages, and similar great things.

This is major flaw in design and fixing it involves removing one line:

collectionKeys.sort();

Can we at least discuss this? I am highly convinced this (possibly) breaking change should affect minimum people and if orderBy worked on objects too, fixing may be very simple on their side.

cztomsik avatar Feb 11 '14 10:02 cztomsik

There was a lot of discussion about it in https://github.com/angular/angular.js/issues/1286

pkozlowski-opensource avatar Feb 11 '14 12:02 pkozlowski-opensource

@cztomsik, major browsers don't all keep the order of keys added to objects in the same way... So there is actually some issues there.

I agree it would be preferable if the orderBy filter supported object collections as well as arrays, though.

I've set this as PRs plz!, because I believe it should be desirable to fix the orderBy filter to support this use case. If anyone wants to have a go at it, please do =)

In the mean time, you could get around this by simply using arrays instead of objects, IMO.

caitp avatar Feb 11 '14 17:02 caitp

+1

mgurov avatar Feb 28 '14 22:02 mgurov

+1

sricc avatar Mar 07 '14 20:03 sricc

+1

kmlx avatar Apr 03 '14 16:04 kmlx

+1

kesarion avatar Apr 04 '14 11:04 kesarion

+1

pungme avatar Apr 15 '14 13:04 pungme

+1

pedrompg avatar Apr 17 '14 13:04 pedrompg

+1 !

bdecarne avatar Apr 29 '14 22:04 bdecarne

+1

CarlosLCervantes avatar May 21 '14 02:05 CarlosLCervantes

+1

eugef avatar May 27 '14 16:05 eugef

+1

kevingermain avatar Jun 05 '14 15:06 kevingermain

+1

ovcharenkovv avatar Jul 16 '14 15:07 ovcharenkovv

+1

CapMousse avatar Jul 17 '14 14:07 CapMousse

+1

mathielen avatar Jul 29 '14 15:07 mathielen

It is very easy to fix this with an toArray filter:

Here is a hard-coded solution to an order by on an object issue: http://jsfiddle.net/q48Vt/

Here is a version that uses @finalclass's toArray filter (remembering that this will not work on IE8): http://jsfiddle.net/z8ME5/

Here is a version that uses toArray but actually modifies the original object (and will work on IE8): http://jsfiddle.net/JHL6X/

I don't expect this to be added to the core but maybe there is enough desire...

petebacondarwin avatar Aug 03 '14 15:08 petebacondarwin

I have created a module that provides the toArray filter for helping with these situations

http://ngmodules.org/modules/angular-toArrayFilter

petebacondarwin avatar Aug 04 '14 10:08 petebacondarwin

So, @cztomsik mentioned there is a line that sorts our objects by keys alphabetically. Is there a reason why angular does this per default? To be honest, I don't want that angular sorts my objects per default (regarding performance and usability).

I don't think introducing a new filter toArray with new limitations for a self-created issue is very practical.

major browsers don't all keep the order of keys added to objects in the same way

@caitp, which browser doesn't keep the order of string indexed object properties? Of course, in the official specs is described that the order is not guaranteed, but I don't know any browser that sort it randomly and makes a custom sort necessary. They even have tests that prove that de-facto existing guarantee because it would otherwise break a lot of applications out there. So it'd be nice to know which browser you mean. I remember the very first version of Google Chrome where they had a random order issue; but they fixed it because all other major browser didn't do it and it broke a lot of stuff. So, as @cztomsik said, we can expect that in the futures all browser will guarantee the order of string indexed object properties and there's definitely no need to sort it explicitly.

If you stick with toArray then make it at least available in the core, please.

marcj avatar Aug 26 '14 17:08 marcj

@marcj: see https://code.google.com/p/v8/issues/detail?id=164 (I'm not sure if behaviour between engines has converged in more recent years, but there's absolutely no guarantee of enumeration order anywhere --- it can change between browser versions, and it's not specified and thus not guaranteed to be the same across browsers)

caitp avatar Aug 26 '14 19:08 caitp

I'm talking about string keys, not number/string mixed keys. Many developer rely on that order because all browsers handle it equal, and this is where the guarantee comes from: No browser wants to introduce a new behavior for that because they would otherwise break a lot of javascript code out there, which makes their browser basically useless. So, you're trying to fix a issue with that .sort() call which doesn't exist and introduce with it a ugly workaround. It just makes no sense to convert a object to a array to have the same result as without the array but with more code and cpu consumption.

A javascript developer should know about that number/string object keys ordering issue and provide a alternative way to handle that, or even the user of a library know how to fix that.

There's no need from angular to force a sort on that object. All those +1 says enough and the web is full of 'How to fix angular ng-repeat object sort'. Its not intuitive, not expected and makes no sense at all. It makes no sense for objects with string keys only (which is primary usage) because it works already out of the box as expected and it makes no sense for mixed because if I know as developer there can be numbers as keys I do a sort() on my own or use other ways to handle that.

The calculation is easy:

  • If you remove that .sort all that headache goes away and users need it are still able to call a .sort() on their object.
  • If you stick with it you'll have every day developers who think 'why on earth do they sort it' until the end of time. The web is already full of 'Angularjs ng-repeat object order' topics. All those +1 comments say enough.

A library should make things simple and KISS, but this decision makes actually the opposite.

marcj avatar Aug 26 '14 21:08 marcj