Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting DOM elements with sort() trouble in IE9-10

I have this code that works perfectly in latest Chrome, Firefox, Opera, but fails in IE9-10:

var div = document.querySelector('#wrap'),
    para = document.querySelectorAll('#wrap p');

var paraArr = [].slice.call( para ).sort(function( a,b ) {
  return a.textContent > b.textContent;
});

paraArr.forEach(function( p ) {
  div.appendChild( p );
});

fiddle: http://jsfiddle.net/2nUMk/1/

Any ideas what the problem is? Is the sort implementation not the same in IE as in other browsers? Is even sort the problem here?

like image 570
elclanrs Avatar asked Dec 15 '25 19:12

elclanrs


1 Answers

There is no need in div.innerHTML = ""; since it removes the sorted elements.

In the sorter function you may explicitly set the returned values:

var paraArr = [].slice.call(para).sort(function (a, b) {
    return a.textContent > b.textContent ? 1 : -1;
});

DEMO: http://jsfiddle.net/2nUMk/3/

like image 87
VisioN Avatar answered Dec 17 '25 11:12

VisioN



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!