firebase Query.orderBy* returns unordered list












0















I'm running tests to understands the behavior of of firebase Queries.



this is general function to return the query passed to it:



function logVal( query , tag ){
return query.on('value', function(snapshot) {
console.log( tag , snapshot.val());
});
}


in this test examples , the returned list is the same as my original data with no ordering!.



sRef = firebase.database().ref("students");
logVal ( "orderByChild" , sRef.orderByChild("bio") ) ;
logVal ( "orderByChild" , sRef.orderByChild("chem") ) ;
logVal ( "orderByChild" , sRef.orderByChild("physics") ) ;
logVal ( "orderByKey", sRef.orderByKey());
logVal ( "orderByValue" , sRef.orderByValue() );


this is my model data



{
students: {


"fahd": {
"physics": 9,
"chem" : 2,
"bio" : 0
}
,
"nasser": {
"physics": 8,
"chem" : 7,
"bio" : 6
}

,

"ahmad": {
"physics": 7,
"chem" : 5,
"bio" : 9
},
"ali": {
"physics": 9,
"chem" : 9,
"bio" : 9
}
,
"hus": {
"physics": 10,
"chem" : 5,
"bio" : 6
}

}
}
;









share|improve this question

























  • Can you show the actual output so we can see for ourselves exactly what's happening? And can we see what sRef is?

    – Doug Stevenson
    Jan 1 at 21:48













  • sRef = firebase.database().ref("students"); & the output is the same as data models

    – tabebqena
    Jan 1 at 21:57
















0















I'm running tests to understands the behavior of of firebase Queries.



this is general function to return the query passed to it:



function logVal( query , tag ){
return query.on('value', function(snapshot) {
console.log( tag , snapshot.val());
});
}


in this test examples , the returned list is the same as my original data with no ordering!.



sRef = firebase.database().ref("students");
logVal ( "orderByChild" , sRef.orderByChild("bio") ) ;
logVal ( "orderByChild" , sRef.orderByChild("chem") ) ;
logVal ( "orderByChild" , sRef.orderByChild("physics") ) ;
logVal ( "orderByKey", sRef.orderByKey());
logVal ( "orderByValue" , sRef.orderByValue() );


this is my model data



{
students: {


"fahd": {
"physics": 9,
"chem" : 2,
"bio" : 0
}
,
"nasser": {
"physics": 8,
"chem" : 7,
"bio" : 6
}

,

"ahmad": {
"physics": 7,
"chem" : 5,
"bio" : 9
},
"ali": {
"physics": 9,
"chem" : 9,
"bio" : 9
}
,
"hus": {
"physics": 10,
"chem" : 5,
"bio" : 6
}

}
}
;









share|improve this question

























  • Can you show the actual output so we can see for ourselves exactly what's happening? And can we see what sRef is?

    – Doug Stevenson
    Jan 1 at 21:48













  • sRef = firebase.database().ref("students"); & the output is the same as data models

    – tabebqena
    Jan 1 at 21:57














0












0








0








I'm running tests to understands the behavior of of firebase Queries.



this is general function to return the query passed to it:



function logVal( query , tag ){
return query.on('value', function(snapshot) {
console.log( tag , snapshot.val());
});
}


in this test examples , the returned list is the same as my original data with no ordering!.



sRef = firebase.database().ref("students");
logVal ( "orderByChild" , sRef.orderByChild("bio") ) ;
logVal ( "orderByChild" , sRef.orderByChild("chem") ) ;
logVal ( "orderByChild" , sRef.orderByChild("physics") ) ;
logVal ( "orderByKey", sRef.orderByKey());
logVal ( "orderByValue" , sRef.orderByValue() );


this is my model data



{
students: {


"fahd": {
"physics": 9,
"chem" : 2,
"bio" : 0
}
,
"nasser": {
"physics": 8,
"chem" : 7,
"bio" : 6
}

,

"ahmad": {
"physics": 7,
"chem" : 5,
"bio" : 9
},
"ali": {
"physics": 9,
"chem" : 9,
"bio" : 9
}
,
"hus": {
"physics": 10,
"chem" : 5,
"bio" : 6
}

}
}
;









share|improve this question
















I'm running tests to understands the behavior of of firebase Queries.



this is general function to return the query passed to it:



function logVal( query , tag ){
return query.on('value', function(snapshot) {
console.log( tag , snapshot.val());
});
}


in this test examples , the returned list is the same as my original data with no ordering!.



sRef = firebase.database().ref("students");
logVal ( "orderByChild" , sRef.orderByChild("bio") ) ;
logVal ( "orderByChild" , sRef.orderByChild("chem") ) ;
logVal ( "orderByChild" , sRef.orderByChild("physics") ) ;
logVal ( "orderByKey", sRef.orderByKey());
logVal ( "orderByValue" , sRef.orderByValue() );


this is my model data



{
students: {


"fahd": {
"physics": 9,
"chem" : 2,
"bio" : 0
}
,
"nasser": {
"physics": 8,
"chem" : 7,
"bio" : 6
}

,

"ahmad": {
"physics": 7,
"chem" : 5,
"bio" : 9
},
"ali": {
"physics": 9,
"chem" : 9,
"bio" : 9
}
,
"hus": {
"physics": 10,
"chem" : 5,
"bio" : 6
}

}
}
;






javascript firebase firebase-realtime-database






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 1 at 21:56







tabebqena

















asked Jan 1 at 21:39









tabebqenatabebqena

3171312




3171312













  • Can you show the actual output so we can see for ourselves exactly what's happening? And can we see what sRef is?

    – Doug Stevenson
    Jan 1 at 21:48













  • sRef = firebase.database().ref("students"); & the output is the same as data models

    – tabebqena
    Jan 1 at 21:57



















  • Can you show the actual output so we can see for ourselves exactly what's happening? And can we see what sRef is?

    – Doug Stevenson
    Jan 1 at 21:48













  • sRef = firebase.database().ref("students"); & the output is the same as data models

    – tabebqena
    Jan 1 at 21:57

















Can you show the actual output so we can see for ourselves exactly what's happening? And can we see what sRef is?

– Doug Stevenson
Jan 1 at 21:48







Can you show the actual output so we can see for ourselves exactly what's happening? And can we see what sRef is?

– Doug Stevenson
Jan 1 at 21:48















sRef = firebase.database().ref("students"); & the output is the same as data models

– tabebqena
Jan 1 at 21:57





sRef = firebase.database().ref("students"); & the output is the same as data models

– tabebqena
Jan 1 at 21:57












2 Answers
2






active

oldest

votes


















2














When you call snapshot.val() the results are converted into a JSON object. And the order of properties in a JSON objected is by definition undefined.



So if you want to show the results in order, you need to process them using snapshot.forEach:



return query.on('value', function(snapshot) {
snapshot.forEach(function(child) {
console.log( tag , child.val());
});
})




The second problem is that orderByValue will only work if the child nodes have a primitive value. Since your nodes under users are JSON objects themselves, they don't have a value that can be sorted on, and thus the nodes are returned in an unspecified order.






share|improve this answer
























  • thanks Frank; that is exactly my answer . Please take a look to the suggested edit in it.

    – tabebqena
    Jan 1 at 22:14



















0














I find partial Solution :



when chnging the logVal function to :



function logVal( tag, query ){
return query.on('value', function(snapshot) {
console.log(tag)
snapshot.forEach(function(snapshot) {
var val = snapshot.val();
console.log("student: " + snapshot.key +
"bio: " + val.bio +
"physics: " + val.physics +
" chem: " + val.chem )
});
});
}


the output is differ in orderByChild methods. it success in arranging student according to the value of the passed key : ex chem



it still return the same data model in orderByKey & orderByValue.



the error in the original logVal function, may be becouse that dictionary object is non sorted object. so when we call console.log() the whole object is converted to array which loss the order.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53999153%2ffirebase-query-orderby-returns-unordered-list%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    When you call snapshot.val() the results are converted into a JSON object. And the order of properties in a JSON objected is by definition undefined.



    So if you want to show the results in order, you need to process them using snapshot.forEach:



    return query.on('value', function(snapshot) {
    snapshot.forEach(function(child) {
    console.log( tag , child.val());
    });
    })




    The second problem is that orderByValue will only work if the child nodes have a primitive value. Since your nodes under users are JSON objects themselves, they don't have a value that can be sorted on, and thus the nodes are returned in an unspecified order.






    share|improve this answer
























    • thanks Frank; that is exactly my answer . Please take a look to the suggested edit in it.

      – tabebqena
      Jan 1 at 22:14
















    2














    When you call snapshot.val() the results are converted into a JSON object. And the order of properties in a JSON objected is by definition undefined.



    So if you want to show the results in order, you need to process them using snapshot.forEach:



    return query.on('value', function(snapshot) {
    snapshot.forEach(function(child) {
    console.log( tag , child.val());
    });
    })




    The second problem is that orderByValue will only work if the child nodes have a primitive value. Since your nodes under users are JSON objects themselves, they don't have a value that can be sorted on, and thus the nodes are returned in an unspecified order.






    share|improve this answer
























    • thanks Frank; that is exactly my answer . Please take a look to the suggested edit in it.

      – tabebqena
      Jan 1 at 22:14














    2












    2








    2







    When you call snapshot.val() the results are converted into a JSON object. And the order of properties in a JSON objected is by definition undefined.



    So if you want to show the results in order, you need to process them using snapshot.forEach:



    return query.on('value', function(snapshot) {
    snapshot.forEach(function(child) {
    console.log( tag , child.val());
    });
    })




    The second problem is that orderByValue will only work if the child nodes have a primitive value. Since your nodes under users are JSON objects themselves, they don't have a value that can be sorted on, and thus the nodes are returned in an unspecified order.






    share|improve this answer













    When you call snapshot.val() the results are converted into a JSON object. And the order of properties in a JSON objected is by definition undefined.



    So if you want to show the results in order, you need to process them using snapshot.forEach:



    return query.on('value', function(snapshot) {
    snapshot.forEach(function(child) {
    console.log( tag , child.val());
    });
    })




    The second problem is that orderByValue will only work if the child nodes have a primitive value. Since your nodes under users are JSON objects themselves, they don't have a value that can be sorted on, and thus the nodes are returned in an unspecified order.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Jan 1 at 22:12









    Frank van PuffelenFrank van Puffelen

    241k29384412




    241k29384412













    • thanks Frank; that is exactly my answer . Please take a look to the suggested edit in it.

      – tabebqena
      Jan 1 at 22:14



















    • thanks Frank; that is exactly my answer . Please take a look to the suggested edit in it.

      – tabebqena
      Jan 1 at 22:14

















    thanks Frank; that is exactly my answer . Please take a look to the suggested edit in it.

    – tabebqena
    Jan 1 at 22:14





    thanks Frank; that is exactly my answer . Please take a look to the suggested edit in it.

    – tabebqena
    Jan 1 at 22:14













    0














    I find partial Solution :



    when chnging the logVal function to :



    function logVal( tag, query ){
    return query.on('value', function(snapshot) {
    console.log(tag)
    snapshot.forEach(function(snapshot) {
    var val = snapshot.val();
    console.log("student: " + snapshot.key +
    "bio: " + val.bio +
    "physics: " + val.physics +
    " chem: " + val.chem )
    });
    });
    }


    the output is differ in orderByChild methods. it success in arranging student according to the value of the passed key : ex chem



    it still return the same data model in orderByKey & orderByValue.



    the error in the original logVal function, may be becouse that dictionary object is non sorted object. so when we call console.log() the whole object is converted to array which loss the order.






    share|improve this answer




























      0














      I find partial Solution :



      when chnging the logVal function to :



      function logVal( tag, query ){
      return query.on('value', function(snapshot) {
      console.log(tag)
      snapshot.forEach(function(snapshot) {
      var val = snapshot.val();
      console.log("student: " + snapshot.key +
      "bio: " + val.bio +
      "physics: " + val.physics +
      " chem: " + val.chem )
      });
      });
      }


      the output is differ in orderByChild methods. it success in arranging student according to the value of the passed key : ex chem



      it still return the same data model in orderByKey & orderByValue.



      the error in the original logVal function, may be becouse that dictionary object is non sorted object. so when we call console.log() the whole object is converted to array which loss the order.






      share|improve this answer


























        0












        0








        0







        I find partial Solution :



        when chnging the logVal function to :



        function logVal( tag, query ){
        return query.on('value', function(snapshot) {
        console.log(tag)
        snapshot.forEach(function(snapshot) {
        var val = snapshot.val();
        console.log("student: " + snapshot.key +
        "bio: " + val.bio +
        "physics: " + val.physics +
        " chem: " + val.chem )
        });
        });
        }


        the output is differ in orderByChild methods. it success in arranging student according to the value of the passed key : ex chem



        it still return the same data model in orderByKey & orderByValue.



        the error in the original logVal function, may be becouse that dictionary object is non sorted object. so when we call console.log() the whole object is converted to array which loss the order.






        share|improve this answer













        I find partial Solution :



        when chnging the logVal function to :



        function logVal( tag, query ){
        return query.on('value', function(snapshot) {
        console.log(tag)
        snapshot.forEach(function(snapshot) {
        var val = snapshot.val();
        console.log("student: " + snapshot.key +
        "bio: " + val.bio +
        "physics: " + val.physics +
        " chem: " + val.chem )
        });
        });
        }


        the output is differ in orderByChild methods. it success in arranging student according to the value of the passed key : ex chem



        it still return the same data model in orderByKey & orderByValue.



        the error in the original logVal function, may be becouse that dictionary object is non sorted object. so when we call console.log() the whole object is converted to array which loss the order.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 1 at 22:11









        tabebqenatabebqena

        3171312




        3171312






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53999153%2ffirebase-query-orderby-returns-unordered-list%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            How to fix TextFormField cause rebuild widget in Flutter

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith