'Why my bubble sort dosnt work when i add Drag and Drop?
When I added Drag and Drop, sorting by dates and letters stopped working for me. I understand that this is due to the fact that I do sorting when I do "map". And I add a new sorting method for dragged objects there. How can this be fixed?
This is my Drag and Drop function:
function dragStartHandler(e, item) {
console.log(item)
setCurrentItem(item)
}
function dragEndHandler(e) {
}
function dragOVerHandler(e) {
e.preventDefault()
}
function dragHandler(e, item) {
e.preventDefault()
setTable([...table].map(c => {
if (c.id === item.id) {
return { ...c, order: currentItem.order }
}
if (c.id === currentItem.id) {
return { ...c, order: item.order }
}
return c;
}))
}
const SortItems = (a, b) => {
if (a.order > b.order) {
return 1
} else {
return -1
}
}
Then i sort my array and use sort.
table.sort(SortItems).map((item, id) => {
return (
<tr
onDragStart={(e) => dragStartHandler(e, item)}
onDragLeave={(e) => dragEndHandler(e)}
onDragEnd={(e) => dragEndHandler(e)}
onDragOver={(e) => dragOVerHandler(e)}
onDrop={(e) => dragHandler(e, item)}
draggable={true}
key={item.id}>
But now my other sort function doesn't work: 
useEffect(() => {
setTable((table) => ([...table].sort((a, b) => !arrays ? a.data.year - b.data.year || a.month - b.month : b.data.year - a.data.year || b.month -
a.month)))
}, [arrays])
const sortByText = () => {
setTable((table) => ([...table].sort((a, b) => a.text.toLocaleLowerCase > b.text.toLocaleLowerCase ? 1 : -1)))
}
How can i fix this?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
