Newbie here how can I sort all of the following table row attach an onclick listener on the header after it is displayed.
ID | Name | Inventory Volume |
---|---|---|
1 | Rachel | Data is not enough |
2 | Ross | 100 |
3 | Monica | 1 |
4 | Connor | Data is not enough |
5 | Dustin | -5 |
into this sorting example is in descending, the words should be the last.
ID | Name | Inventory Volume |
---|---|---|
2 | Ross | 100 |
3 | Monica | 10 |
5 | Dustin | -5 |
1 | Rachel | Data is not enough |
4 | Connor | Data is not enough |
But I also want the other columns to be sorted as well and the other columns to have the function to sort as well.
Tried this solution but only works for the column. https://jsfiddle.net/7wnke5q2/
function sortData(data, method) {
let lessData = 'Data Not Enough'
let lastItems = []
let sortedList;
if (method == 'descending') {
sortedList = data.sort((a, b) => {
return a - b
})
} else if (method == 'ascending') {
sortedList = data.sort((a, b) => {
return b - a
})
}
for (let i = 0; i < sortedList.length; i++) {
if (sortedList[i] == lessData) {
let item = sortedList[i]
sortedList.splice(i, 1)
sortedList.push(item)
}
}
sortedList = sortedList.concat(lastItems)
return sortedList
}
Could you please help me out. Thanks in advance!