1

I have a Firebase structure like

categories {
  "1234" : {
    title : "Object 1",
    categories : {
      "7654" : {
        title : "Object 7",
        categories : {
          "ABCD" : {
            title : "Object A"
          }
        }
      },
      "8765" : {
        title : "Object 8"
      },
      "9876" : {
        title : "Object 9"
      }
    }
  },
  "2345" : {
    title : "Object 2"
  }
}

Now I want to get all categories incl. the subcategories. My main problem is, that the subcategories (7654, 8765, 9876) is a map (which is internally ordered by the key-hash). But I need to have them in the order by key-values.

Is the only possible way to create the object with DataSnapshot.getValue(MyObject) and to sort it in the client? Or do I need to create my structure in a different way?

mars3142
  • 2,501
  • 4
  • 28
  • 58
  • Sorting in Firebase http://stackoverflow.com/questions/42249524/sorting-items-position-using-firebaserecycleradapter and http://stackoverflow.com/questions/26700924/query-based-on-multiple-where-clauses-in-firebase?noredirect=1&lq=1 – Khaledonia Mar 17 '17 at 13:50
  • While you can order on sub-properties, that only works in practice if those sub-properties are at a fixed path. So `categoriesRef.orderByChild("metadata/creationDate")` would work. But `categoriesRef.orderByChild("categories/???/title")` does not. Sorting in the client would work, but you'd end up downloading more data than needed. The solution is to change your data model to allow the query. See http://stackoverflow.com/questions/40656589/firebase-query-if-child-of-child-contains-a-value. – Frank van Puffelen Mar 17 '17 at 13:54

0 Answers0