sorting - How to sort a cursor with variables in android? -
i have listview
(in listframent
) cursoradapter
shows different names of places. @ begining, list sorted names (using order_by in query).
now, have added item_row.xml textview
containing distance of place user's position latitude/longitude of each place , locationmanager
. , sort list of places distance (from nearest farthest).
the problem calculate distance outside "sql environment", , there no methods sort cursor
/listview
/adapter
...
any ideas ?
use collections.sort(list, comparator)
custom comparator
.
example using class sorting sum of both integer fields:
import java.util.arraylist; import java.util.collections; import java.util.comparator; import java.util.list; class { final string name; final int somenumber; final int someothernumber; a(final string name, final int a, final int b) { this.name = name; this.somenumber = a; this.someothernumber = b; } @override public string tostring() { return this.name; } } class yourcomparator implements comparator<a> { @override public int compare(a o1, o2) { final int sumo1 = o1.somenumber + o1.someothernumber; final int sumo2 = o2.somenumber + o2.someothernumber; return (sumo1 - sumo2); } } public class main { public static void main(final string args[]) { final list<a> listofas = new arraylist<a>(); listofas.add(new a("three", 1, 2)); listofas.add(new a("two", 1, 1)); listofas.add(new a("ten", 3, 7)); listofas.add(new a("five", 3, 2)); collections.sort(listofas, new yourcomparator()); system.out.println(listofas); } }