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 >>

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
Different types of Access Modifiers
public: Any thing declared as public can be accessed from anywhere.

private: Any thing declared as private can't be seen outside of its class.

protected: Any thing declared as protected can be accessed by classes in the same package and subclasses in the other packages.

default modifier: Can be accessed only to classes in the same package.
Famous Quotations
If you don’t make mistakes, you’re not working on hard enough problems. And that’s a big mistake.
-- Frank Wilczek

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].

Most Visited Pages

Other Interesting Sites

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