Skip to content

Instantly share code, notes, and snippets.

@AlaaZarifa
Last active September 16, 2017 10:03
Show Gist options
  • Save AlaaZarifa/f03400c35f32288eb9f5b25391aed2de to your computer and use it in GitHub Desktop.
Save AlaaZarifa/f03400c35f32288eb9f5b25391aed2de to your computer and use it in GitHub Desktop.
sort arraylist items..
if (homeData.size() > 0) {
Collections.sort(homeData,
Collections.reverseOrder(new SortedListAdapterCallback<HomeItemModel>(adapter) {
@Override
public int compare(HomeItemModel o1, HomeItemModel o2) {
// arraylist of strings that contoins what the user had slected as hobbies
// whihc are the ones that I want them to show always at the top..!
if (Hawk.contains("Hobbies")) {
ArrayList<String> hobbiesList = Hawk.get("Hobbies");
if (o1.getCategory().equals("Swimming") && hobbiesList.contains("Swimming")) {
weight1 = 5;
} else if (o1.getCategory().equals("Football") && hobbiesList.contains("Football")) {
weight1 = 4;
} else if (o1.getCategory().equals("Running") && hobbiesList.contains("Running")) {
weight1 = 3;
} else if (o1.getCategory().equals("MMA") && hobbiesList.contains("MMA")) {
weight1 = 2;
} else if (o1.getCategory().equals("D&D") && hobbiesList.contains("D&D")) {
weight1 = 1;
}else {
weight1 = 0;
}
if (o2.getCategory().equals("Swimming") && hobbiesList.contains("Swimming")) {
weight2 = 5;
} else if (o2.getCategory().equals("Football") && hobbiesList.contains("Football")) {
weight2 = 4;
} else if (o2.getCategory().equals("Running") && hobbiesList.contains("Running")) {
weight2 = 3;
} else if (o2.getCategory().equals("MMA") && hobbiesList.contains("MMA")) {
weight2 = 2;
} else if (o2.getCategory().equals("D&D")&& hobbiesList.contains("D&D")) {
weight2 = 1;
} else {
weight2 = 0;
}
if (weight1 == weight2) {
return 0;
} else if (weight1 > weight2) {
return 1;
} else {
return -1;
}
}
return 0;
}
@Override
public boolean areContentsTheSame(HomeItemModel oldItem, HomeItemModel newItem) {
return false;
}
@Override
public boolean areItemsTheSame(HomeItemModel item1, HomeItemModel item2) {
return false;
}
}));
}
adapter.notifyDataSetChanged();
// code..
b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment