Current firebase database is:
{
"user_type" : {
"number1" : {
"name" : "Puneet",
},
"number2" : {
"name" : "AMAN SINGH",
},
"number3" : {
"name" : "harsha",
},
"number4" : {
"name" : "abhishek",
},
"number5" : {
"name" : "Satya",
},
}
}
I want to sort the list by name in ascending order. So for this code is :
myRef.child("user_type").orderByChild("name");
But it is returning the result as:
{
"admin" : {
"number2" : {
"name" : "AMAN SINGH",
},
"number1" : {
"name" : "Puneet",
},
"number5" : {
"name" : "Satya",
},
"number4" : {
"name" : "abhishek",
},
"number3" : {
"name" : "harsha",
}
}
}
But I am expecting the following order:
{
"admin" : {
"number2" : {
"name" : "AMAN SINGH",
},
"number4" : {
"name" : "abhishek",
},
"number3" : {
"name" : "harsha",
}
"number1" : {
"name" : "Puneet",
},
"number5" : {
"name" : "Satya",
}
}
}
So instead of the current order, it is sorting first capital case letter words and then it is sorting small letter words and merging the list.
The order you're seeing is exactly what I would expect. This is because of the way strings naturally sort. UTF-8 strings with ascii data sort according to the ascii byte value of each character. As you can see from this table, all capital letters sort before all lowercase values. This explains the sort order you're seeing.
If you want the sort order to be alphabetical without regard to case, you will need to store a version of the name string with case normalized. It's common to store strings to sort like this in all lowercase. For example, instead of "AMAN SINGH", you would store "aman singh" in a special child used only for sorting.
Related
when I use OrderByChild or key with startAt or equalTo it returns wrong value
This is with equalTo
String diy = intent.getStringExtra(PARCELABLE_KEY);
Query qr = dr.child(AddDIYFragment.DIY_CHILD).orderByChild("title").equalTo(diy);
This is with startAt
Query searchQueryNum2 = reference.child(AddDIYFragment.DIY_CHILD).orderByChild("title").startAt(text);
searchQueryNum2.addListenerForSingleValueEvent(this);
I try orderByKey but it also doesnt work
EDIT JSON TREE EXAMPLE(EDited but I cand add more)
{
"gg0" : {
"category" : "lifestyle",
"eq" : "SMTH",
"title" : "gg"
},
"hdhshs5" : {
"category" : "Sports",
"eq" : "hshsj",
"title" : "hdhshs"
},
"hdhshsjsjs6" : {
"category" : "Sports",
"eq" : "eooe",
"title" : "hdhshsjsjs"
},
"java1" : {
"category" : "Sports",
"eq" : "hshjs",
"title" : "java"
},
"jshsjs7" : {
"category" : "Sports",
"eq" : "hdhshsjjs",
"title" : "jshsjs"
},
"jsjsjsjs9" : {
"category" : "Sports",
"eq" : "hshshwhw",
"title" : "jsjsjsjs"
},
"papajs8" : {
"category" : "Sports",
"eq" : "hdhshhajasjjs",
"title" : "papajs"
},
"sjs4" : {
"category" : "Sports",
"eq" : "hdhs",
"title" : "sjs"
}
}
EDIT onDataChange:-
this is a simple startAt
Query searchQueryNum2 = reference.child(AddDIYFragment.DIY_CHILD).orderByChild("title").startAt("gg");
Log.d(TAG, "query is made");
searchQueryNum2.addListenerForSingleValueEvent(this);
And onDataChange
private ArrayList<Prod> list = new ArrayList<>();
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot temp : dataSnapshot.getChildren()) {
list.add(temp.getValue(Prod.class));
Log.d(TAG, "list added something " + list.get(list.size() - 1));
}
}
EDIT
I set the child to be orderd by startat "gg" and as you see in the top we have gg but it return also (hdhshs,java,jsjsjsjs,papajs ,sjs, etc)
Your query is:
...orderByChild("title").startAt("gg")
This is executed by Firebase in the following way:
Order all children of the reference by their title.
Find the child whose title start with gg, or the first one after that if no child starts with gg.
Start returning results from there, until we run out of results.
I think you're looking for a startsWith(...) operator, which Firebase doesn't natively have, but can easily be created by combining startAt() and endAt().
...orderByChild("title").startAt("gg").endAt("gg\uf8ff")
With this, you're adding an extra step to the recipe above:
Stop returning results once we reach a title that is greater than gg\uf8ff.
{
"Events" : {
"events" : {
"-KT5UMQAhDHs1bB8bKLc" : {
"activity" : "biking",
"address" : "54°05'45.8\"N 28°19'02.9\"E, просп. Мира 9, Жодино, Беларусь",
"category" : "sport",
"creatorID" : "m11EvlP19OSbEz8XYl2MsNCwGXX2",
"date" : 1475431200689,
"forFriends" : true,
"info" : "вттчтвт",
"name" : "ьаьатвтвтт",
"uid" : "-KT5UMQAhDHs1bB8bKLc"
},
"-KUSsl7RaL-dVoDCoSYT" : {
"activity" : "picnic",
"address" : "Узденский район, Узденский район, Беларусь",
"category" : "entertainment",
"creatorID" : "1RXDSJzSEXakxvNdM41Ae3nbHN72",
"date" : 1477328400000,
"forFriends" : false,
"info" : "45",
"name" : "Test of date",
"uid" : "-KUSsl7RaL-dVoDCoSYT"
}
},
"FriendsOF" : {
"NuKweeGv8zTgt1Vi9RcU3i1u86U2" : {
"1RXDSJzSEXakxvNdM41Ae3nbHN72" : true
},
"m11EvlP19OSbEz8XYl2MsNCwGXX2" : {
"1RXDSJzSEXakxvNdM41Ae3nbHN72" : true
}
},
"Users" : {
"1RXDSJzSEXakxvNdM41Ae3nbHN72" : {
"email" : "*#gmail.com",
"name" : "Алексей Гвоздицкий",
"picture" : "*.jpg"
},
"NuKweeGv8zTgt1Vi9RcU3i1u86U2" : {
"email" : "*#gmail.com",
"name" : "Ольга Гвоздицкая",
"picture" : "*.jpg"
},
"m11EvlP19OSbEz8XYl2MsNCwGXX2" : {
"email" : "*#gmail.com",
"name" : "Егор Александров",
"picture" : "*.jpg"
}
}
}
I have a list of "events". Every "event" can be "forFiends" only (only if current user is in event creators "FriendsOF" list, then he can see this event. FriendsOF it's a whitelist). All availiable for current user events i want to represent in RecyclerView.
I am stucked. I can get all data snapshot and then filter on client side it. But it is not way of Jedi, because i load tonns of unnecessary data. Another solution is to deny acces to event if user should not see it. But i don't understand how to do it. My ref is mEventsRef = FirebaseDatabase.getInstance().getReference().child("Events").child("events"); And rule i can make for
"events":{
"$eventId":{".read":....}
}
So according to this guide i'll get empty recyclerView because of "error callback triggered with PERMISSION_DENIED"
Help me to solve my problem please!
I am making an app that should include chat. I would like to display conversations of the current user. I am not sure how to get only chats of the current user. This is my structure of database.
"chats" : {
"-KWYiJiFwE6miPNkKPe0" : {
"chatPartner1" : {
"id" : "rtg3ru7zNYQQp9LA484VIG5rItl1",
"name" : "ghjg",
"photo" : ""
},
"chatPartner2" : {
"id" : "rtg3ru7bzwwewNYQwwewetegb",
"name" : "petra",
"photo" : ""
},
"id" : "-KWYiJiFwE6miPNkKPe0",
"lastMessage" : "dhjcd",
"timestamp" : 1479141836276
}
This json represents storing message for specified chats according to its id.
"messages" : {
"-KWYiJiFwE6miPNkKPe0" : {
"-KWYiJiPWR3n_YcvokCm" : {
"senderId" : "rtg3ru7zNYQQp9LA484VIG5rItl1",
"text" : "dhjcd",
"timestamp" : 1479141836276
}
}
I would need to check if the chatPartner 1 or 2 contains id of the current user. Or do you know about better way to store chats?
Thanks
Having the following data:
"animals" : {
"a1" : {
"name" : "Dog"
},
"a2" : {
"name" : "Cat"
},
"a3" : {
"name" : "Cow"
}
},
"users" : {
"u1" : {
"favAnimals" : {
"a1" : true,
"a2" : true
},
"name" : "Danilo"
}
}
How can I display for the user, using the FirebaseListAdapter, the complete list of animals, highlighing user's favorite animals?
Is it possible to query and join them, to get a Database Reference that would watch for changes both in "/animals" and "/users/$uid/favAnimals" and return something like:
"animals" : {
"a1" : {
"name" : "Dog",
"fav" : true
},
"a2" : {
"name" : "Cat"
"fav" : true
},
"a3" : {
"name" : "Cow"
}
}
I wish to retrieve only the top 50 messages a user has within the database, with schema:
"individualMessages" : {
"99e4989b-a046-4c5f-9478-5ebd8bdc3ded" : {
"a96da7b1-7c4e-44bc-b82e-fc75bed52bcd" : {
"-KBUjngi9JStE-k4RMUJ" : {
"from" : "a96da7b1-7c4e-44bc-b82e-fc75bed52bcd",
"messageText" : "hey",
"timeSent" : "2016-02-26 17:40:46"
},
"-KBYhsRONfGxVI7Ysb3F" : {
"from" : "a96da7b1-7c4e-44bc-b82e-fc75bed52bcd",
"messageText" : "pool",
"timeSent" : "2016-02-27 12:10:52"
}
},
"fdb17f3a-7b7d-4aa5-9a0b-b9fb33c349de" : {
"-KBcYtQ5gD-drFZqz_MU" : {
"from" : "fdb17f3a-7b7d-4aa5-9a0b-b9fb33c349de",
"messageText" : "ouu",
"timeSent" : "2016-02-28 10:46:52"
},
"-KBcYw24M-9-7H5A3czD" : {
"from" : "fdb17f3a-7b7d-4aa5-9a0b-b9fb33c349de",
"messageText" : "gsf",
"timeSent" : "2016-02-28 10:47:03"
}
However, simply using:
individualMessageMyPath = myFeastFirebase.child("individualMessages").child(.....
getTop50Messages = individualMessageMyPath.limitToLast(5);
getTop50Messages.orderByChild("timeSent").addChildEventListener(mainMessagingListener);
Returns all messages on that node.
Reviewing the documentation this should work, however it does not.
What is missing?