I'm trying to load set of data to a list in ascending order. I have DistanceObject
object and its as shown below,
DistanceObject mObject = new DistanceObject();
mObject.setOutletname(object.getString("LocationName"));
mObject.setAddress(object.getString("LocationAddress"));
mObject.setDistance(strdistance);
mListObjects.add(mObject);
Loading data to listview
I can use below shown code and load the data to listview without sorting the list,
adapter = new CustomListOutlets(ActivityListOutlets.this,
mListObjects);
lv.setAdapter(adapter);
My problem is I want to sort the strdistance
and load the data to a ListView
. I can sort the strdistance
using Collections.sort()
but with the sorting of strdistance
I want to sort its LocationName
and LocationAddress
.
Any help will be appreciated to achieve this task, Thanks in advance.
Updated
final List<DistanceObject> mListObjects = new ArrayList<DistanceObject>();
DistanceObject mObject = new DistanceObject();
mObject.setOutletname(object.getString("LocationName"));
mObject.setAddress(object.getString("LocationAddress"));
mObject.setDistance(strdistance);
Collections.sort(mListObjects, new Comparator<DistanceObject>() {
public int compare(DistanceObject s1, DistanceObject s2) {
if (Integer.parseInt(s1.getDistance()) < Integer.parseInt(s2.getDistance()))
return 1;
if (Integer.parseInt(s1.getDistance()) > Integer.parseInt(s2.getDistance()))
return -1;
else
return 0;
}
});
}
lv = (ListView) findViewById(R.id.list_outlets);
lv.setTextFilterEnabled(true);
adapter = new CustomListOutlets(ActivityListOutlets.this,
mListObjects);
lv.setAdapter(adapter);