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

I have this code, which works fine in recent browsers Chrome, Firefox, Opera, but does not work 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 );
});

      

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

Any ideas what the problem is? Is the implementation sort

not the same in IE as in other browsers? Is there even a problem here sort

?

+3


source to share


1 answer


No need for div.innerHTML = "";

as it removes sorted items.

In the sorter function, you can explicitly set the return values:



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

      

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

+2


source







All Articles