Reputation: 49
I am building recipes app. I have page in my app that retrieves all the recipes from the database on Firebase into ListView. Each Recipe has these variables:
public String key;
public String uid;
public String title;
public String type;
public String ingredients;
public String instructions;
public int likes;
Here's the function that retrieves all the data :
//Retrieve Data from database
public void retriveData() {
database.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
recipes = new ArrayList<Recipe>();
for(DataSnapshot data : dataSnapshot.getChildren()) {
Recipe r = data.getValue(Recipe.class);
recipes.add(r);
}
allRecipeAdapter = new AllRecipeAdapter(AllRecipeActivity.this,0,0,recipes);
lv.setAdapter(allRecipeAdapter);
}
}
}
Now i want to create another screen that also has a ListView and i would like to sort the Recipes by nubmer of likes and then enter the top 10 recipes into the ListView. I searched on Google i found the function OrderByValue but i can't figure out how to use it i realized how to works but i can't implement this to my project.
Upvotes: 0
Views: 235
Reputation: 598797
Gaëtan Maisse and KLHauser gave you solutions that work in pure client-side code. Here's an alternative that uses Firebase Database queries, which means the ordering and filtering happen on the server:
// assuming that database refers to the list of all recipe
Query topRecipes = database.orderByChild("likes").limitToLast(10);
topRecipes.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
recipes = new ArrayList<Recipe>();
for(DataSnapshot data : dataSnapshot.getChildren()) {
Recipe r = data.getValue(Recipe.class);
// add the recipe to the front of the list
recipes.add(0, r);
}
allRecipeAdapter = new AllRecipeAdapter(AllRecipeActivity.this,0,0,recipes);
lv.setAdapter(allRecipeAdapter);
}
}
Since Firebase queries always sort items in ascending order, the query takes the last 10 items - those are the ones with the highest like count. To reverse those items, the code inserts each item to the from of the list, instead of appending them to the end.
Upvotes: 1
Reputation: 876
Java7:
Collections.sort(list, new Comparator<Recipe>() {
@Override
public int compare(Recipe r1, Recipe r2) {
if (r1.getLikes() > r2.getLikes())
return 1;
if (r1.getLikes() < r2.getLikes()
return -1;
return 0;
}
});
Java8 (using lambda):
recipes.stream()
.sorted((r1, r2) -> Integer.compare(r1.getLikes(),
r2.getLikes()))
Upvotes: 1
Reputation: 12347
You can sort your list using a Comparator
and then get a sublist of 10 first elements before displaying it in the ListView
:
Comparator<Recipe> likesOrderComparator = new Comparator<Recipe>() {
public int compare(Recipe recipe1, Recipe recipe2) {
return recipe1.likes < recipe2.likes ? -1 : recipe1.likes == recipe2.likes ? 0 : 1;
}
};
Collections.sort(recipes, likesOrderComparator);
List<Recipe> topTenRecipes = recipes.subList(0, 9);
// Now display topTenRecipes in a ListView
Upvotes: 1