Skip to content

Instantly share code, notes, and snippets.

@androidneha
Created February 28, 2023 12:50
Show Gist options
  • Save androidneha/e1483ff01c7da80a17e7912c32a4f954 to your computer and use it in GitHub Desktop.
Save androidneha/e1483ff01c7da80a17e7912c32a4f954 to your computer and use it in GitHub Desktop.
List<String> fetchItemToDisplay(List<List<String>> items, int sortParameter, int sortOrder, int itemsPerPage, int pageNumber) {
List<String> output = new ArrayList<>();
Collections.sort(items, (a, b) -> {
if (sortOrder == 0) {
return sortParameter != 0 ?
Integer.parseInt(a.get(sortParameter)) - Integer.parseInt(b.get(sortParameter)) :
a.get(sortParameter).compareTo(b.get(sortParameter));
} else {
return sortParameter != 0 ?
Integer.parseInt(b.get(sortParameter)) - Integer.parseInt(a.get(sortParameter)) :
b.get(sortParameter).compareTo(a.get(sortParameter));
}
});
int sum = itemsPerPage * pageNumber;
if (sum >= items.size()) {
return output;
}
for (int i = sum; i <items.size() && i < sum + itemsPerPage; i++) {
List<String> temp = items.get(i);
output.add(temp.get(0));
}
return output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment