one option would be not using linked list, but System.Collections.Generic.List<T>
another would be implementing System.Collections.Generic.IEnumerable<T>
and constructor from System.Collections.Generic.IEnumerable<T>
on your linked list, like this:
using System;
using System.Collections.Generic;
using System.Linq;
class MyList<T> : IEnumerable<T> {
class ListElement {
public T content;
public ListElement next;
}
ListElement head = null;
public MyList() {
head = null;
}
public MyList(IEnumerable<T> values)
: this() {
ListElement last = null;
foreach(var v in values) {
ListElement that = new ListElement { content = v };
if(last != null) {
last.next = that;
} else {
head = that;
}
last = that;
}
}
public IEnumerator<T> GetEnumerator() {
var current = head;
while(current != null) {
yield return current.content;
current = current.next;
}
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
return this.GetEnumerator();
}
then,
public MyList<T> sortedBy<TK>(Func<T, TK> key) {
return new MyList<T>(this.OrderBy(key));
}
}
and
var sortedLinkedList = unsortedLinkedList.sortedBy(item => item.Property);
if you want sorting to mutate your list, not make new; or actually sort linked list, not convert to array and sort that (IEnumerable.OrderBy seems to do that),
What's the fastest algorithm for sorting a linked list? might be of some use