Hmm,
Then, I guess I would have to write sth like this:
class ddd {
public int i;
public string name;
public ddd(int i, string name) {
this.i = i;
this.name = name;
}
(... implement Icomparable)
}
LinkedList<ddd a = new LinkedList<ddd >();
a.AddLast( ... a ddd ...);
a.AddLast(... a ddd ...);
a.AddLast(... a ddd ...);
List<ddd b = new List<ddd >();
foreach (ddd item in a) {
b.Add(item);
}
b.Sort();
a.Clear();
foreach (ddd item in b) {
a.AddLast(item);
}
or this:
class ddd {
public int i;
public string name;
public ddd(int i, string name) {
this.i = i;
this.name = name;
}
}
LinkedList<ddda = new LinkedList<ddd>();
a.AddLast( ... a ddd ...);
a.AddLast( ... a ddd ...);
a.AddLast( ... a ddd ...);
SortedList<int, dddb = new SortedList<int, ddd>();
foreach (ddd item in a)
b.Add(item.i, item);
a.Clear();
foreach (ddd item in b.Values)
a.AddLast(item);
foreach (ddd item in a)
Console.WriteLine(item.i + " " + item.name);
So, just wandering ... Is there a better way to make the conversion
LinkedList -List in order to sort it?
Are the two ways above equivalent?
The second way should require more memory, but perhaps is faster?
--
With regards,
Nick
"Michael A. Covington" <lo**@ai.uga.edu.for.addresswrote in message
news:u5**************@TK2MSFTNGP04.phx.gbl...
>
"Gaurav Vaish (www.edujini-labs.com)"
<ga*****************@nospam.gmail.comwrote in message
news:uz**************@TK2MSFTNGP04.phx.gbl...
>I know how to use Icomparable.
However, I can't figure out how to sort a generic linked list? (without
writing the algorithm)
There's no way out using inbuilt API.
You'll need to write your own... or there's a way around:
1. Convert LinkedList into an array.
2. Sort the array
3. Convert the array back to LinkedList
That is actually a good approach. Linked lists are inherently not
sortable without a lot of extra work linking and unliking things. Arrays
are very sortable.