I
I
Insolation2018-03-04 21:47:19
JavaScript
Insolation, 2018-03-04 21:47:19

Why are the rows not sorted?

There is this script:

static sortRows() {
    const tbody = document.getElementsByTagName('tbody')[0];
    let rows = [];

    for (let i = 0; i < tbody.children.length; i++) {
      rows.push(tbody.children[i]);
    }

    console.log(rows);

    rows.sort((a, b) => {
      return a.innerHTML - b.innerHTML;
    });

    for (let i = 0; i < rows.length; i++) {
      tbody.appendChild(rows[i]);
    }
  }

It should sort the rows in the table. But why it does not work, I seem to have written everything correctly. Maybe you see an error?
Whole code:
https://codepen.io/anon/pen/pamygR

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
Coder321, 2018-03-05
@Coder321

sort((item1, item1)=>{
if (item1 < item1)
  return -1
if ( item1 > item1)
  return 1
return 0
})

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question