Sort a Double LinkedList in Java

This code is for showing how to sort a Double LinkedList in Java. You can use sort(List<T> list) method. It will sort the double list in ascending order. If you want to sort it in descending order, you can use sort(List<T> list, Comparator<? super T> c) .

public static void main(String[] args) {
	LinkedList<Double> doubleList = new LinkedList<Double>();
	doubleList.add(2.2);
	doubleList.add(1.1);
	doubleList.add(3.3);
	System.out.println(doubleList);
 
	//sort in ascending order
	Collections.sort(doubleList);
	System.out.println(doubleList);
 
	//sort in descending order
	Collections.sort(doubleList, Collections.reverseOrder());
	System.out.println(doubleList);	
}

Output:

[2.2, 1.1, 3.3]
[1.1, 2.2, 3.3]
[3.3, 2.2, 1.1]

1 thought on “Sort a Double LinkedList in Java”

Leave a Comment