I am needing to sort a linked list alphabetically. I have a Linked List full of passengers names and need the passengers name to be sorted alphabetically. How would one do this? Anyone have any references or videos?
9 Answers
You can use Collections#sort
to sort things alphabetically.

- 43,520
- 33
- 120
- 170
-
4If it's a `List
` there's no need for a custom comparator. – Matt Ball Jun 06 '11 at 02:09 -
humm collections.sort cool. public static LinkedList
NameList1 = new LinkedList – allencoded Jun 06 '11 at 02:12(); is what I am trying to sort. Thanks -
3@allencoded, all you need to do is `Collections.sort(NameList1);`, and then your list will be sorted alphabetically. – mre Jun 06 '11 at 02:17
In order to sort Strings alphabetically you will need to use a Collator
, like:
LinkedList<String> list = new LinkedList<String>();
list.add("abc");
list.add("Bcd");
list.add("aAb");
Collections.sort(list, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return Collator.getInstance().compare(o1, o2);
}
});
Because if you just call Collections.sort(list)
you will have trouble with strings that contain uppercase characters.
For instance in the code I pasted, after the sorting the list will be: [aAb, abc, Bcd]
but if you just call Collections.sort(list);
you will get: [Bcd, aAb, abc]
Note: When using a Collator
you can specify the locale Collator.getInstance(Locale.ENGLISH)
this is usually pretty handy.

- 3,199
- 4
- 34
- 57

- 1,357
- 1
- 12
- 18
-
interesting...I was unaware that the sorting of strings was sensitive to upper/lower casing... – mre Jun 06 '11 at 02:32
-
1Better invoke `getInstance()` only once, not once for each compare call. (I just saw that Collator implements `Comparator – Paŭlo Ebermann Jun 06 '11 at 10:36
-
@Paülo: you are right, but the idea was to create a simple code to paste (not the most efficient one). The perfect scenario (if you sort all the time) will be a static reference to Comparator.getInstance() – Federico Vera Jun 06 '11 at 15:35
-
@mre yeahp, this usually create some nasty behavior when users enter their own name in the system (some use uppercase, some lowercase) and lists are never properly sorted. – Federico Vera Jun 06 '11 at 15:37
-
Or: Collections.sort(list, (o1, o2) -> Collator.getInstance().compare(o1, o2)); – Maksim Nov 29 '16 at 16:48
In java8 you no longer need to use Collections.sort method as LinkedList inherits the method sort from java.util.List, so adapting Fido's answer to Java8:
LinkedList<String>list = new LinkedList<String>();
list.add("abc");
list.add("Bcd");
list.add("aAb");
list.sort( new Comparator<String>(){
@Override
public int compare(String o1,String o2){
return Collator.getInstance().compare(o1,o2);
}
});
References:
http://docs.oracle.com/javase/8/docs/api/java/util/LinkedList.html
http://docs.oracle.com/javase/7/docs/api/java/util/List.html

- 7,619
- 1
- 30
- 51
-
2In java8 you can use a lambda too: `list.sort((a, b) -> Collator.getInstance().compare(a, b))`. Sadly, `list::sort` does not support natural sorting without an explicit `Comparator`. – Dávid Horváth May 27 '20 at 17:14
Elegant solution since JAVA 8:
LinkedList<String>list = new LinkedList<String>();
list.add("abc");
list.add("Bcd");
list.add("aAb");
list.sort(String::compareToIgnoreCase);
Another option would be using lambda expressions:
list.sort((o1, o2) -> o1.compareToIgnoreCase(o2));

- 4,404
- 2
- 25
- 42
Here is the example to sort implemented linked list in java without using any standard java libraries.
package SelFrDemo;
class NodeeSort {
Object value;
NodeeSort next;
NodeeSort(Object val) {
value = val;
next = null;
}
public Object getValue() {
return value;
}
public void setValue(Object value) {
this.value = value;
}
public NodeeSort getNext() {
return next;
}
public void setNext(NodeeSort next) {
this.next = next;
}
}
public class SortLinkList {
NodeeSort head;
int size = 0;
NodeeSort add(Object val) {
// TODO Auto-generated method stub
if (head == null) {
NodeeSort nodee = new NodeeSort(val);
head = nodee;
size++;
return head;
}
NodeeSort temp = head;
while (temp.next != null) {
temp = temp.next;
}
NodeeSort newNode = new NodeeSort(val);
temp.setNext(newNode);
newNode.setNext(null);
size++;
return head;
}
NodeeSort sort(NodeeSort nodeSort) {
for (int i = size - 1; i >= 1; i--) {
NodeeSort finalval = nodeSort;
NodeeSort tempNode = nodeSort;
for (int j = 0; j < i; j++) {
int val1 = (int) nodeSort.value;
NodeeSort nextnode = nodeSort.next;
int val2 = (int) nextnode.value;
if (val1 > val2) {
if (nodeSort.next.next != null) {
NodeeSort CurrentNext = nodeSort.next.next;
nextnode.next = nodeSort;
nextnode.next.next = CurrentNext;
if (j == 0) {
finalval = nextnode;
} else
nodeSort = nextnode;
for (int l = 1; l < j; l++) {
tempNode = tempNode.next;
}
if (j != 0) {
tempNode.next = nextnode;
nodeSort = tempNode;
}
} else if (nodeSort.next.next == null) {
nextnode.next = nodeSort;
nextnode.next.next = null;
for (int l = 1; l < j; l++) {
tempNode = tempNode.next;
}
tempNode.next = nextnode;
nextnode = tempNode;
nodeSort = tempNode;
}
} else
nodeSort = tempNode;
nodeSort = finalval;
tempNode = nodeSort;
for (int k = 0; k <= j && j < i - 1; k++) {
nodeSort = nodeSort.next;
}
}
}
return nodeSort;
}
public static void main(String[] args) {
SortLinkList objsort = new SortLinkList();
NodeeSort nl1 = objsort.add(9);
NodeeSort nl2 = objsort.add(71);
NodeeSort nl3 = objsort.add(6);
NodeeSort nl4 = objsort.add(81);
NodeeSort nl5 = objsort.add(2);
NodeeSort NodeSort = nl5;
NodeeSort finalsort = objsort.sort(NodeSort);
while (finalsort != null) {
System.out.println(finalsort.getValue());
finalsort = finalsort.getNext();
}
}
}

- 35
- 1
- 6
Node mergeSort(Node head) {
if(head == null || head.next == null) {
return head;
}
Node middle = middleElement(head);
Node nextofMiddle = middle.next;
middle.next = null;
Node left = mergeSort(head);
Node right = mergeSort(nextofMiddle);
Node sortdList = merge(left, right);
return sortdList;
}
Node merge(Node left, Node right) {
if(left == null) {
return right;
}
if(right == null) {
return left;
}
Node temp = null;
if(left.data < right.data) {
temp = left;
temp.next = merge(left.next, right);
} else {
temp = right;
temp.next = merge(left, right.next);
}
return temp;
}
Node middleElement(Node head) {
Node slow = head;
Node fast = head;
while (fast != null && fast.next != null && fast.next.next != null) {
fast = fast.next.next;
slow = slow.next;
}
return slow;
}

- 2,689
- 24
- 20
You can do it by Java 8 lambda expression :
LinkedList<String> list=new LinkedList<String>();
list.add("bgh");
list.add("asd");
list.add("new");
//lambda expression
list.sort((a,b)->a.compareTo(b));

- 21
- 2
I wouldn't. I would use an ArrayList or a sorted collection with a Comparator. Sorting a LinkedList is about the most inefficient procedure I can think of.

- 305,947
- 44
- 307
- 483
-
6Collections.sort uses a workaround there: it copies the contents to an array, sorts the array and copies it back. This will not take much longer than for an ArrayList. – Paŭlo Ebermann Jun 06 '11 at 10:39
-
2May be it's true, but that's still an implementation detail. EJP's answer makes sense as it warns of the potential danger of using a wrong data structure. Makes sense, +1. – nawfal Jun 05 '14 at 14:05
-
@Ilya_Gazman If that's addressed to me, it's not what the question is about. – user207421 Feb 14 '16 at 07:16
If you'd like to know how to sort a linked list without using standard Java libraries, I'd suggest looking at different algorithms yourself. Examples here show how to implement an insertion sort, another StackOverflow post shows a merge sort, and ehow even gives some examples on how to create a custom compare function in case you want to further customize your sort.