elclanrs
elclanrs

Reputation: 94121

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?

Upvotes: 3

Views: 176

Answers (1)

VisioN
VisioN

Reputation: 145428

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/

Upvotes: 2

Related Questions