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]
Category >> Collections  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
<pre><code> 
String foo = "bar";
</code></pre>
  • SoftTechguy

    let we have a linklist of size n stored info are name and id what is the best way to sort ? job interview