Skip to content

Instantly share code, notes, and snippets.

@pipiscrew
Created June 16, 2014 13:12
Show Gist options
  • Save pipiscrew/532d63b3197c97cd6250 to your computer and use it in GitHub Desktop.
Save pipiscrew/532d63b3197c97cd6250 to your computer and use it in GitHub Desktop.
[android] Sort List<t>
//at activity
List<TestItem> rowItems = new ArrayList<TestItem>();
TestItem rec;
for (DataSnapshot snapshot : arg0.getChildren()) {
rec = new TestItem();
rec.setID(snapshot.getRef().getName());
if (snapshot.child("last_date_used").getValue() != null)
{
rec.setLast_date_used(Long.parseLong(snapshot.child("last_date_used").getValue().toString()));
}
else
rec.setLast_date_used(0L);
rowItems.add(rec);
}
Collections.sort(rowItems);
listAdapter.notifyDataSetChanged();
//the class
public class TestItem implements Comparable{
private String id;
private long last_date_used;
public long getLast_date_used() {
return last_date_used;
}
public void setLast_date_used(long last_date_used) {
this.last_date_used = last_date_used;
}
.
.
.
//sort DESC
@Override
public int compareTo(Object o) {
TestItem f = (TestItem)o;
if (last_date_used < f.getLast_date_used()) {
return 1;
}
else if (last_date_used > f.getLast_date_used()) {
return -1;
}
else {
return 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment