Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OrderBy is sorting differently in Chrome than other browsers

I noticed that orderBy in Chrome is not sorting the same as it is in other browsers.

I am writing for a project in Angular 1.3.

Chrome sorting: Chrome

IE 10 sorting: enter image description here

There is no special logic regarding specific browsers and both arrays are EXACTLY the same.

The predicate is explicitly defined:

$scope.predicate = 'style';
$scope.reverse = true;

My ng-repeat looks like this:

<tr ng-repeat="line in model.resultList | orderBy:predicate:reverse">

There's nothing special or different with my code. The 'sizes', however, are strings (e.g. size: '8-', ..., size: '6',...). Why is this sorting entirely differently on Chrome than on every other browser?

All of my results are coming in sorted from the back end in groups of style # and then ascending sizes. When the primary sort is set on Angular, in this case Style, Chrome is mixing up the already sorted sizes. Please see Plnk below (open in Chrome and IE/FireFox to see the difference)

http://plnkr.co/edit/6TO8pZZ8jZ8Tytw6wHpQ?p=preview

like image 769
developthewebz Avatar asked May 12 '16 14:05

developthewebz


1 Answers

Nothing wrong. Since you're sorting by other field - size could be shuffled in any manner. orderBy relies on buit-in sort function of browser (source). Browser implementations could be different. And sort shouldn't be stable (MDN, ES2015 Standard section).

If you want to achieve consistent behavior use sorting by multiple fields. Like

orderBy:[predicate, 'size']:reverse
like image 98
Andrey Avatar answered Oct 23 '22 04:10

Andrey