Sort a String LinkedList in Java

Sorting a string LinkedList in Java is easy. You can sort the string LinkedList in ascending alphabetical order by using sort(List<T> list) . You can also sort the string LinkedList in descending alphabetical order by using sort(List<T> list, Comparator<? super T> c) .

Here is the code:

public static void main(String[] args) {
	LinkedList<String> stringList = new LinkedList<String>();
	stringList.add("a");
	stringList.add("c");
	stringList.add("b");
	System.out.println(stringList);
 
	//sort in ascending order
	Collections.sort(stringList);
	System.out.println(stringList);
 
	//sort in descending order
	Collections.sort(stringList, Collections.reverseOrder());
	System.out.println(stringList);	
}

Output:

[a, c, b]
[a, b, c]
[c 1=”b,” 2=”a” language=”,”][/c]

Leave a Comment