Skip to content

Instantly share code, notes, and snippets.

@tranchausky
Created January 13, 2021 08:26
Show Gist options
  • Select an option

  • Save tranchausky/789bef19574adba358b7b74b1e2a328e to your computer and use it in GitHub Desktop.

Select an option

Save tranchausky/789bef19574adba358b7b74b1e2a328e to your computer and use it in GitHub Desktop.
Sort firebase data in descending
function sortDescObj(list, key) {
var sortedKeys = Object.keys(list).sort(function(a,b) {
return list[a][key] > list[b][key] ? -1:1
});
var newObjectSort = []
var tem=[]
for (let index = 0; index < sortedKeys.length; index++) {
tem=[]
newObjectSort.push(list[sortedKeys[index]])
}
return newObjectSort
}
//time: new Date().getTime(),
//firebase test get result sort time
contactsRef.orderByChild('time').on("value", function(snapshot) {
allContacts = snapshot.val()
var newObjectSort = sortDescObj(allContacts,'time')
showContentContact(newObjectSort)
}, function(errorObject) {
console.log("The read failed: " + errorObject.code);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment