Reputation: 633
I have several ArrayLists
that are going into the ListView
adapter. What I'm trying to do is sort by just one of those ArrayLists
...
names = new ArrayList<String>();
locations = new ArrayList<String>();
timedates = new ArrayList<String>();
//arraylists populated here
adapter = new HomeAdapter(getActivity(), names, locations, timedates);
setListAdapter(adapter);
How would I go about sorting the entire ListView
by timedates
for example?
Appreciate any help. Thanks!
Upvotes: 0
Views: 68
Reputation: 140
You have to use a datamodel with only one ArryList. Details are given bellow:
arrayList= new ArrayList<DataModel>();
//arraylists populated here
adapter = new HomeAdapter(getActivity(), arrayList);
setListAdapter(adapter);
DataModel class will look like this:
public class DataModel {
public String names;
public String locations;
public String timedates;
}
Finally sorting:
Collections.sort(arrayList, new SortList());
public class SortList implements Comparator<DataModel> {
public int compare(DataModel arg0, DataModel arg1) {
int flag = arg0.timedates.compareTo(arg1.timedates);
return flag;
}
}
Edit 1:
To add data into the list:
DataModel dataModel = new DataModel();
dataModel.names = "Ashiq";
dataModel.locations = "Khulna, Bangladesh";
dataModel.timedates = System.currentTimeMillis();
arrayList.add(dataModel);
You don't have to use arraylist individually for each item. Just wrap all strings into dataModel and add it to the arraylist for adding each set of data.
Let me know if anything not seem to be clear.
Upvotes: 1
Reputation: 1030
Try this
Collections.sort(timedates,new Comparator<String>(){
@Override
public int compare(String s1, String s2){
return s1.compareTo(s2);
}
});
adapter.notifyDataSetChanged();
Upvotes: 0