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 4, 2018 in Java by anto.trigg4
• 3,420 points
229 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 4, 2018 by geek.erkami
• 2,640 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,190 points
221 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,350 points
77 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 13, 2018 in Java by samarth295
• 2,190 points
116 views
0 votes
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,280 points
67 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,880 points
140 views
0 votes
1 answer

How do I join 2 lists

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

answered May 10, 2018 in Java by sharth
• 3,350 points
32 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,350 points
434 views
0 votes
2 answers

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

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

answered Aug 21 in Java by Sirajul
• 37,930 points
82 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 8, 2018 in Java by geek.erkami
• 2,640 points
417 views