7

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
5
  • 2
    The output seems the same as the output in the requirement. So what exactly is the problem? Commented Aug 22, 2015 at 21:02
  • I don't quite understand the question. What's not working? Commented Aug 22, 2015 at 21:02
  • I need to assign the sorted array in ArrayList<Model> Commented Aug 22, 2015 at 21:06
  • Can you explain the error. What is the output of your code? Commented Aug 22, 2015 at 21:19
  • I am getting Incompatible type error. Commented Aug 22, 2015 at 21:22

3 Answers 3

57

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
Sign up to request clarification or add additional context in comments.

3 Comments

can you tell for descending order
@Prasad Change lhs and rhs positions.
Thank you so much for the solution, it works well and saved much of my time!
7
 Collections.sort(actorsList, new Comparator<Actors>() {
      @Override
      public int compare(Actors lhs, Actors rhs) {
       return lhs.getName().compareTo(rhs.getName());
     }
    });

1 Comment

this code is work properly i will use this code....this code work in string & also integer value
1
Collections.sort(modelList, (lhs, rhs) -> lhs.getName().compareTo(rhs.getName()));

Comments

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.