When I add the JSON file to Firebase all the data gets arranged randomly
Note:- for some issues previously I don't have any parent node, so what I mean is that after the root node there are all child nodes
as you can see the quotes are not arranged in numerical order,how my JSON is
what should I do and please give a solution without adding a parent node because it is creating other difficulties
CodePudding user response:
You cannot and should not rely on the ordering of elements within a JSON object. From the JSON specification at https://www.json.org/
An object is an unordered set of name/value pairs
Firebase is a NoSQL database. Hence, you can imaging it more like a hashmap with key being you q1
, q2
.. etc. and value being corresponding quote.
If order matters to you, storing in an array would make more sense.
Thus, your json should look like this:
{
"questions": [
"Whereof one cannot speak, thereof one must be silent – Ludwig Wittgenstein",
"Entities should not be multiplied unnecessarily – William of Ockham"
...
]
}
CodePudding user response:
When I add the JSON file to Firebase all the data gets arranged randomly.
That's not a random order. By default, all the nodes in the Firebase Console are ordered by key. Unfortunately, there is no way you can change that order. Please note that the keys are always strings. And when strings are ordered, the order is lexicographically. This means that it doesn't consider any sort of numeric values when sorting.
That being said, if you order numbers, this is the normal order:
- 1
- 9
- 10
- 11
But when it comes to strings, the order is:
- "1"
- "11"
- "19"
- "9"
Where 19 comes before 9.
If you want to change this behavior, please check my answer in the following post: