Is there a way to rearrange an Object or Objects in Firebase real time database? (Firebase saves it in a sorted manner by keys)
I couldnt figure it out so this is what I am doing on the client side
My loop for step 3 is a little too complicated, is there an effecient, ES6 way to achieve the same?
Step 1: Data from firebase:
[
id1: {
name: 'name1',
index: 3
},
id2: {
name: 'name2',
index: 0
},
id3: {
name: 'name3',
index: 1
},
id4: {
name: 'name4',
index: 2
}
]
Step 2: Sort it
const sortedList = list.sort((a, b) =>
a.val.index > b.val.index ? 1 : b.val.index > a.val.index ? -1 : 0
)
output = [
id2: {
name: 'name2',
index: 0
},
id3: {
name: 'name3',
index: 1
},
id4: {
name: 'name4',
index: 2
}
id1: {
name: 'name1',
index: 3
},
]
Step 3: This is where I need help with a function to update the indexes when moved from one position to another