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
181 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,320 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
121 views
0 votes
1 answer

How can I convert an 'ArrayList<String> to 'String[]' in Java

List<String> list = new ArrayList<String>(); String[] array = ...READ MORE

answered Apr 20, 2018 in Java by Daisy
• 8,040 points
144 views
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,320 points
54 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
70 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
42 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,850 points
65 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,320 points
22 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,320 points
264 views
0 votes
1 answer

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

You can try using this: List<String> list = ...READ MORE

answered May 21, 2018 in Java by geek.erkami
• 2,320 points
60 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,320 points
309 views