Join
Blogs
Questions
Videos
Tags
Members
Search
 
 
Create your own blog, earn points and get popular!

Java Sort ArrayList

This Java Sort ArrayList example shows how to Sort an ArrayList in Java.
  1. import java.util.ArrayList;
  2. import java.util.Collections;
  3.  
  4. //Java Sort ArrayList
  5.  
  6. public class JavaSortArrayList {
  7.    
  8.     public static void main(String args){
  9.        
  10.         //create new ArrayList
  11.         ArrayList<Integer> aListNumbers = new ArrayList<Integer>();
  12.         aListNumbers.add(23);
  13.         aListNumbers.add(11);
  14.         aListNumbers.add(43);
  15.         aListNumbers.add(46);
  16.         aListNumbers.add(98);
  17.         aListNumbers.add(7);
  18.         aListNumbers.add(86);
  19.        
  20.         System.out.println("ArrayList before sort " + aListNumbers);
  21.         /*
  22.          * To sort ArrayList use,
  23.          * Collections.sort(List list) method.
  24.          */
  25.        
  26.         Collections.sort(aListNumbers);
  27.         System.out.println("ArrayList after sort" + aListNumbers);
  28.        
  29.         /*
  30.          * Note: All elements in the ArrayList must implement Comparable interface
  31.          * in order for sort to work. Additionally, none of the elements should throw
  32.          * ClassCastException while being compared with each other.
  33.          *
  34.          * In above example, ArrayList contains all Integer elements, so sort will work
  35.          * by default. If we have an ArrayList which contains objects of custom class,
  36.          * then that class must implement Comparable interface for the sort to work.
  37.          *
  38.          * See the example below.
  39.          */
  40.  
  41.         //create new ArrayList with custom objects of Class Car.
  42.         ArrayList<Car> aListCars = new ArrayList<Car>();
  43.         aListCars.add(new Car("Merc Benz", 3000000));
  44.         aListCars.add(new Car("Maruti Suzuki 800", 280000));
  45.         aListCars.add(new Car("Honda City", 950000));
  46.         aListCars.add(new Car("Tata Nano", 180000));
  47.        
  48.         System.out.println("ArrayList before sort " + aListCars);
  49.  
  50.         //sort ArrayList of Car objects
  51.         Collections.sort(aListCars);
  52.        
  53.         System.out.println("ArrayList after sort " + aListCars);
  54.        
  55.     }
  56. }
  57.  
  58. class Car implements Comparable{
  59.    
  60.     private String name;
  61.     private int price;
  62.    
  63.     public Car(String name, int price){
  64.         this.name = name;
  65.         this.price = price;
  66.     }
  67.    
  68.     public String getName(){
  69.         return this.name;
  70.     }
  71.    
  72.     public int getPrice(){
  73.         return this.price;
  74.     }
  75.    
  76.     //this method must be defined if we are implementing Comparable interface
  77.     public int compareTo(Object otherCar){
  78.        
  79.         if(otherCar instanceof Car){
  80.             throw new ClassCastException("Not a valid Car object!!");
  81.         }
  82.        
  83.         Car tempCar = (Car)otherCar;
  84.        
  85.         if(this.getPrice() > tempCar.getPrice()){
  86.             return 1;
  87.         }else if(this.getPrice() < tempCar.getPrice()){
  88.             return -1;
  89.         }else{
  90.             return 0;
  91.         }
  92.     }
  93. }
Your rating: None Average: 3 (15 votes)
Share this
Anonymous's picture

Dear your sol is the best

Dear your sol is the best sol....thanks ...
Anonymous's picture

Excuse me. I have 2 questions

Excuse me. I have 2 questions for you. 1.Is that a big deal by using a built-in method sort and writing sort program ? 2.If i have too many employees how would i write too many if-else conditions ?
Anonymous's picture

nice solution

nice solution
Anonymous's picture

nice solution

nice solution
Anonymous's picture

good

good
Anonymous's picture

Thx brother! :) easy solution

Thx brother! :) easy solution to my need :) Greetings from Venezuela
Anonymous's picture

I imported into eclipse and

I imported into eclipse and the main method would not run, this is due to the [ ] in main are missing.. String args[] Good example..
Anonymous's picture

hi why I run it program ,it

hi why I run it program ,it get to me error as follow? Exception in thread "main" java.lang.NoSuchMethodError: main

Post new comment