Sorting an ArrayList in Java

0 votes

I have a code with a class name Car.

public class Car{

    private String carName;
    private String carDesc;
    private int quantity;

    public String getCarName() {
        return carName;
    }
    public void setCarName(String carName) {
        this.carName = carName;
    }
    public String getCarDesc() {
        return carDesc;
    }
    public void setCarDesc(String carDesc) {
        this.carDesc = carDesc;
    }
    public int getQuantity() {
        return quantity;
    }
    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }
}

Now, using the for loop,  I am creating a creating a list of class “Car”.

List<Car>  cars= new ArrayList<Car>();

Car car;
for(int i=0;i<100;i++)
{
   car= new car();
   car.setname(...);
   cars.add(car);
}

My list contains a few car names added to it.

Can someone help me in sorting this arrayList based on the car names of each object in the list?

May 5, 2018 in Java by anto.trigg4
• 3,440 points
2,204 views

1 answer to this question.

0 votes

You can easily do this by simply using a  “Comparator”. Check out the code below:

List<Car> cars = new ArrayList<Car>();

Car car;
for(int i = 0; i < 100; i++)
{
  car = new Car();
  car.setname(...);
  cars.add(car);
}

// Sorting
Collections.sort(cars, new Comparator<Car>() {
        @Override
        public int compare(Car car2, Car car1)
        {

            return  car1.carName.compareTo(car2.carName);
        }
    });

Hope this works for you, as it worked for me.

answered May 5, 2018 by geek.erkami
• 2,680 points

Related Questions In Java

0 votes
2 answers

One line initialization of an ArrayList object in Java

In Java 8 or earlier: List<String> string = ...READ MORE

answered Jul 26, 2018 in Java by samarth295
• 2,220 points
4,807 views
0 votes
2 answers
0 votes
1 answer

How can I Sort an ArrayList in Java

You can sort the ArrayList in 2 ...READ MORE

answered Apr 23, 2018 in Java by sharth
• 3,370 points
879 views
0 votes
2 answers

How to sort an ArrayList of custom object by property in Java?

You can Sort using java 8 yourList.sort(Comparator.comparing(Classname::getName)); or  yourList.stream().forEach(a -> ...READ MORE

answered Aug 14, 2018 in Java by samarth295
• 2,220 points
3,051 views
+1 vote
1 answer

Remove objects from an array in Java?

We can use external libraries: org.apache.commons.lang.ArrayUtils.remove(java.lang.Object[] array, int ...READ MORE

answered Jun 26, 2018 in Java by scarlett
• 1,290 points
1,205 views
0 votes
2 answers

Remove duplicate elements from an ArrayList in Java

List<String> al = new ArrayList<>(); // add elements ...READ MORE

answered Jul 26, 2018 in Java by Sushmita
• 6,920 points
1,707 views
0 votes
1 answer

How do I join 2 lists

This is not a simpler method, but ...READ MORE

answered May 11, 2018 in Java by sharth
• 3,370 points
810 views
0 votes
1 answer

Convert comma-separated String to ArrayList?

List<String> alist = Arrays.asList(str.split("\\s*,\\s*")); The above code splits ...READ MORE

answered May 17, 2018 in Java by sharth
• 3,370 points
3,385 views
0 votes
2 answers

How to convert ArrayList<String> to String[] in Java

Try this:  String[] strArray = arrayList.toArray(new Str ...READ MORE

answered Aug 21, 2019 in Java by Sirajul
• 59,230 points
1,022 views
0 votes
1 answer

Why the main() method in Java is always static?

As you might know, static here is ...READ MORE

answered May 9, 2018 in Java by geek.erkami
• 2,680 points
2,115 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP