'Add data to top of branch in firebase

My code is responsible for intercepting the data and sending it to the Realtime Database. The nesting structure is as follows: DeviceID - UserID - SessionID - session parameters - ......

My problem is that when new data is added to the database, they are all sorted differently: DeviceID, UserID, and SessionID in alphabetical order, and the session parameters are sorted by time (that is, the old ones are at the top, the new ones are below).

Can I make it so that when added, the new parameters are at the top of their branch?

Example: There is a device with DeviceID "Samsung".

enter image description here

Further, when adding data, new data will appear at the top or bottom, depending on the alphabet.

enter image description here

But I would like the new data to appear only on top. And so on for each branch.

It's my code:

    class PS() :
    Interceptor {

    @Throws(IOException::class)
    override fun intercept(chain: Interceptor.Chain): Response {
    }

    private fun firebasePush(requestMap: Map<String, Any?>, config: PSConfig): Boolean {
        val db = Firebase.database
        val myRef =
            db.getReference(config.deviceId).
            child(config.userId).
            child(config.sessionId).
            push()
        myRef.setValue(requestMap)
        return true
    }

}

If I didn't understand something, please let me know so I can clarify the question.



Solution 1:[1]

I want to write this in the code so that such an order would be displayed in the Firebase console.

There is no way you can do that. You cannot change in any way the order of the nodes in the Firebase Console. By default, as you already noticed, all the nodes are ordered by key. Keep in mind that all keys in Firebase are strings, and when the strings are ordered, are ordered lexicographically.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Alex Mamo