0

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!

2
  • Please paste code of sorting that you've tried. also give reference of sorting, how you want. Commented Oct 3, 2015 at 1:57
  • try to create model class Commented Oct 3, 2015 at 4:48

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

How would I go about adding my current ArrayLists to that class? Thanks!
Seems pretty clear now. One thing though: how would I get data from the list items for use in the listview adapter? For example, if I wanted to pull dataModel.name for a certain position. Thanks!
DataModel dataModel = (DataModel)arrayList.get(position); String name = dataModel.names;
0

Try this

Collections.sort(timedates,new Comparator<String>(){
    @Override
    public int compare(String s1, String s2){
        return s1.compareTo(s2);
    }
});
adapter.notifyDataSetChanged();

3 Comments

Doesn't that sort JUST that arraylist? Looking to sort the whole listview based on timedates
what you need to do is create a class with name,location and timedates as fields. Create an arraylist of that class and then use the sort method.
@zngb that does sort the arraylist but on using adapter.notifyDataSetChanged() the data in listview is sorted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.