9 is higher than 871?

rowsDuplicate.sort((rowA, rowB) => {
let cellA;
let cellB;

// Get the content of cells
cellA = rowA.querySelectorAll('td')[1].innerHTML;
cellB = rowB.querySelectorAll('td')[1].innerHTML;

// descending order
switch (true) {
case cellA > cellB:
return -1;
case cellA < cellB:
return 1;
case cellA === cellB:
return 0;
}
})
rowsDuplicate.sort((rowA, rowB) => {
let cellA;
let cellB;

// Get the content of cells
cellA = rowA.querySelectorAll('td')[1].innerHTML;
cellB = rowB.querySelectorAll('td')[1].innerHTML;

// descending order
switch (true) {
case cellA > cellB:
return -1;
case cellA < cellB:
return 1;
case cellA === cellB:
return 0;
}
})
I have this sort, and for some reason 9 is greater than 871 for some reason. Is there a way to make it so that it reads the entire number not just the first digit?
2 Replies
13eck
13eck3y ago
Sort of…but it's actually that "9" is greater than "871". Everything JS gets from HTML is a string, so it's comparing strings. If you Number([cell]) or parseInt([cell], 10) you'll get numbers and it will compare the way you expect.
kingtigerknight
kingtigerknightOP3y ago
AHHHHH thanks. Forgot it's strings so I need to parse it.
Want results from more Discord servers?
Add your server