Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort ArrayList<Object> in Ascending order android

I have a requirement to sort ArrayList<Object>(Custom objects) in Ascending order based upon name. For that purpose I am using comparator method as like

My ArrayList :

ArrayList<Model> modelList = new ArrayList<Model>();

Code I am using:

   Comparator<Model> comparator = new Comparator<Model>() {
            @Override
            public int compare(CarsModel lhs, CarsModel rhs) {

                String  left = lhs.getName();
                String right = rhs.getName();

                return left.compareTo(right);

            }
        };

   ArrayList<Model> sortedModel = Collections.sort(modelList,comparator);

//While I try to fetch the sorted ArrayList, I am getting error message 

I am completely stuck up and really dont know how to proceed further in order to get sorted list of ArrayList<Object>. Please help me with this. Any help and solutions would be helpful for me. I am posting my exact scenario for your reference. Thanks in advance.

Example:

ArrayList<Model> modelList = new ArrayList<Model>();
modelList.add(new Model("chandru"));
modelList.add(new Model("mani"));
modelList.add(new Model("vivek"));
modelList.add(new Model("david"));

Normal List:

for(Model mod : modelList){
  Log.i("names", mod.getName());
}

Output :

chandru
mani
vivek
david

My requirement after sorting to be like:

for(Model mod : modelList){
  Log.i("names", mod.getName());
}

Output :

chandru
david
mani
vivek
like image 914
Chandru Avatar asked Aug 22 '15 20:08

Chandru


People also ask

How can you sort an ArrayList of objects?

In the main() method, we've created an array list of custom objects list, initialized with 5 objects. For sorting the list with the given property, we use the list's sort() method. The sort() method takes the list to be sorted (final sorted list is also the same) and a comparator.

How do I sort an ArrayList in alphabetical order on Android?

To sort the ArrayList, you need to simply call the Collections. sort() method passing the ArrayList object populated with country names. This method will sort the elements (country names) of the ArrayList using natural ordering (alphabetically in ascending order).

How do you sort an ArrayList in an ArrayList?

An ArrayList can be sorted in two ways ascending and descending order. The collection class provides two methods for sorting ArrayList. sort() and reverseOrder() for ascending and descending order respectively.


1 Answers

Your approach was right. Make your Comparator inner like in the following example (or you can create a new class instead):

ArrayList<Model> modelList = new ArrayList<>();
modelList.add(new Model("chandru"));
modelList.add(new Model("mani"));
modelList.add(new Model("vivek"));
modelList.add(new Model("david"));

Collections.sort(modelList, new Comparator<Model>() {
    @Override
    public int compare(Model lhs, Model rhs) {
        return lhs.getName().compareTo(rhs.getName());
    }
});

output:

chandru
david
mani
vivek
like image 94
Sahar Avr Avatar answered Oct 21 '22 03:10

Sahar Avr