JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

Program: How to sort ArrayList using Comparator?


Description:

This example gives you how to sort an ArrayList using Comparator. The ArrayList contains user defined objects. By using Collections.sort() method you can sort the ArrayList. You have to pass Comparator object which contains your sort logic. The example sorts the Empl objects based on highest salary.


Code:
package com.java2novice.arraylist;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class MyArrayListSort {
	
	public static void main(String a[]){
		
		List<Empl> list = new ArrayList<Empl>();
		list.add(new Empl("Ram",3000));
		list.add(new Empl("John",6000));
		list.add(new Empl("Crish",2000));
		list.add(new Empl("Tom",2400));
		Collections.sort(list,new MySalaryComp());
		System.out.println("Sorted list entries: ");
		for(Empl e:list){
			System.out.println(e);
		}
	}
}

class MySalaryComp implements Comparator<Empl>{

	@Override
	public int compare(Empl e1, Empl e2) {
		if(e1.getSalary() < e2.getSalary()){
			return 1;
		} else {
			return -1;
		}
	}
}

class Empl{
	
	private String name;
	private int salary;
	
	public Empl(String n, int s){
		this.name = n;
		this.salary = s;
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getSalary() {
		return salary;
	}
	public void setSalary(int salary) {
		this.salary = salary;
	}
	public String toString(){
		return "Name: "+this.name+"-- Salary: "+this.salary;
	}
}

Output:
Sorted list entries: 
Name: John-- Salary: 6000
Name: Ram-- Salary: 3000
Name: Tom-- Salary: 2400
Name: Crish-- Salary: 2000
<< Previous Program | Next Program >>
blog comments powered by Disqus

List Of All ArrayList Sample Programs:

  1. Basic ArrayList Operations.
  2. How to read all elements in ArrayList by using iterator?
  3. How to copy or clone a ArrayList?
  4. How to add all elements of a list to ArrayList?
  5. How to delete all elements from my ArrayList?
  6. How to find does ArrayList contains all list elements or not?
  7. How to copy ArrayList to array?
  8. How to get sub list from ArrayList?
  9. How to sort ArrayList using Comparator?
  10. How to reverse ArrayList content?
  11. How to shuffle elements in ArrayList?
  12. How to swap two elements in a ArrayList?
  13. How to convert list to csv string format?
Knowledge Centre
What is java static import?
By using static imports, we can import the static members from a class rather than the classes from a given package. For example, Thread class has static sleep method, below example gives an idea:

import static java.lang.Thread;
public class MyStaticImportTest {
public static void main(String[] a) {
try{
sleep(100);
} catch(Exception ex){

}
}
}

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected] or you can comment on the page.

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 6 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2014 by Nataraja Gootooru. All Rights Reserved.