JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Checkout for Promo Codes

Program: How to sort LinkedList using Comparator?


Description:

This example gives you how to sort an LinkedList using Comparator. The LinkedList contains user defined objects. By using Collections.sort() method you can sort the LinkedList. 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.linkedlist;

import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;

public class MyLinkedListSort {

	public static void main(String a[]){
		
		LinkedList<Empl> list = new LinkedList<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 LinkedList Sample Programs:

  1. Basic LinkedList Operations.
  2. How to read all elements in LinkedList by using iterator?
  3. How to copy or clone a LinkedList?
  4. How to add all elements of a list to LinkedList?
  5. How to delete all elements from my LinkedList?
  6. How to find does LinkedList contains all list elements or not?
  7. How to copy LinkedList to array?
  8. How to get sub list from LinkedList?
  9. How to sort LinkedList using Comparator?
  10. How to reverse LinkedList content?
  11. How to shuffle elements in LinkedList?
  12. How to swap two elements in a LinkedList?
  13. How to convert list to csv string format?
  14. How to add element at first position in LinkedList?
  15. How to add element at last position in LinkedList?
  16. How to read first element from LinkedList?
  17. How to read last element from LinkedList?
  18. How to iterate through LinkedList in reverse order?
  19. LinkedList push(), pop() operations examples.
  20. How to remove elements from LinkedList?
Knowledge Centre
Can you list serialization methods?
Serialization interface does not have any methods. It is a marker interface. It just tells the JVM that your class can be serializable.
Famous Quotations
There is a great difference between worry and concern. A worried person sees a problem, and a concerned person solves a problem.
-- Harold Stephens

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 © 2020 by Nataraja Gootooru. All Rights Reserved.