Nani536
Nani536

Reputation: 25

Listview sorting by priority wise

I had a list view containing 5 arrays, 4 arrays are static arrays and 1 array is dynamic array coming from the server.

My question is how can i sort the entire list view using 1 array(dynamic)

My Adapter is like this

Adapter adapter=new Contacts_Adapter(class.this, Contacts_Names_Array, Contacts_Numbers_Array, Contacts_Images_Array, Contacts_Ids_Array, Chat_List_Array);

in the above code contain 5 arrays

Static Arrays = Contacts_Names_Array, Contacts_Numbers_Array, Contacts_Images_Array, Contacts_Ids_Array

Dynamic Array : Chat_List_Array

Based on the dynamic array i want sort the list view

Any suggestion.....?

Upvotes: 0

Views: 418

Answers (1)

inmyth
inmyth

Reputation: 9050

If you merge all those arrays into one list of POJO objects whose members are contactName, contactNumber,..., contactId then you can use Collections.sort to sort the list.

Merging is simply creating a class like this

class Bla{
    String contactName, contactNumber;
    int contactId;
}

which you fill with the values from your arrays. And then put together the results in one list. If you use Adapter then you have likely done this as Adapter only corresponds to one list of objects.

To sort based on id, you define a Comparator

public class BlaIdComparator<Bla> implements Comparator<Bla> {

     public int compare(Bla bla1, Bla bla2){
          return bla1.contactId - bla2.contactId;
          //this depends on your sorting order, ascending or descending
     }
}

then use it on your list like this

Collections.sort(list, new BlaIdComparator());

then refresh your Adapter for the change to take effect

adapter.notifyDatasetChanged();

Upvotes: 1

Related Questions