-1

Solution:

I have to use snap.value. now I had tried "snap.children" which works fine. In the same query snap.value return unsorted data snap.children return sorted data

self.referenceData = (Database.database().reference().child("imagelist" + "/" + userID!))
    self.referenceData.queryOrdered(byChild: "created").observeSingleEvent(of: .value, with: { (snap : DataSnapshot)  in
  print(snap.value) // Unsorted
  print(snap.children) // Sorted
    ...
})

PROBLEM

I am trying to sort the data by created the key. but nothing happened, maybe any mistake in my code,

My database view Database view

self.referenceData = (Database.database().reference().child("imagelist" + "/" + userID!))
    self.referenceData.queryOrdered(byChild: "created").observeSingleEvent(of: .value, with: { (snap : DataSnapshot)  in
    ...
})
Syscall
  • 19,327
  • 10
  • 37
  • 52
RiTeSh
  • 513
  • 3
  • 12
  • 1
    what are u getting? – Peter Haddad Mar 31 '20 at 11:35
  • when I am saying that, sorting is not working, that only means that same data while using sorting and not using sorting. also, I had written that "nothing happened" – RiTeSh Mar 31 '20 at 12:09
  • "nothing happened" is very vague. It could mean that you don't have any output or something else. Please try to improve your question explaining what exactly you expect and what you see. See also [ask]. – koen Mar 31 '20 at 12:34
  • Please follow the rules of Stack Overflow when your question has been solved. Putting SOLVED in the title, or typing a solution in your question are not the way to do this on Stack Overflow. If you've found a different solution than the accepted answer, post another answer and accept that. If the accepted answer gave you what you needed, but you want to share a bit more on your solution, either leave a comment on that answer, or edit it to add the information. From a quick glance it seems that your solution should probably be a comment on my answer. – Frank van Puffelen Mar 31 '20 at 16:20

1 Answers1

1

Most likely you're calling snap.value, which returns the child nodes in a dictionary. And the entries in a dictionary are by definition unordered.

To get the results in the order that you queried them in, you need to iterate over snap.children.

See my answer here for an example: How to properly use queryOrderedByValue

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • thank you, it helps me, I have to use snap.value. now I had tried "snap.children" which works fine. In a same query snap.value return unsorted data snap.children return sorted data – RiTeSh Mar 31 '20 at 15:52