Sort a LinkedList

  • Thread starter Thread starter Philluminati
  • Start date Start date
P

Philluminati

I have a LinkedList of network objects defined:

LinkedList<NetworkObject>

and I want to sort the contents against one of the properties. I'm not
looking for people to post page long algorithms on how to write a
sorting algorithm, I just want to know reuse as much as possible.
(It's a client app and I only to sort 200 or so records in a one time
operation so it doesn't matter if it takes 30 seconds to do)

Is there some Collection.Sort( myList ) function where I can just have
the sorting algorithm do the work and I just implement the comparer
function in the class? I don't care if it's quick sort or bubble sort.
I just want the simplest, least effort way of writing this code in
this language.

Can anyone help please?

Thanks

Phill
 
Philluminati said:
I have a LinkedList of network objects defined:

LinkedList<NetworkObject>

and I want to sort the contents against one of the properties. I'm not
looking for people to post page long algorithms on how to write a
sorting algorithm, I just want to know reuse as much as possible.
(It's a client app and I only to sort 200 or so records in a one time
operation so it doesn't matter if it takes 30 seconds to do)

Is there some Collection.Sort( myList ) function where I can just have
the sorting algorithm do the work and I just implement the comparer
function in the class? I don't care if it's quick sort or bubble sort.
I just want the simplest, least effort way of writing this code in
this language.

Can anyone help please?

I'd create a new List<NetworkObject> from the LinkedList, then sort
that, and replace the contents of the original LinkedList. That way you
don't have to implement any of the sorting yourself.

To sort against a particular property, use an anonymous method to
specify a Comparison<NetworkObject>.
 
I'd create a new List<NetworkObject> from the LinkedList, then sort
that, and replace the contents of the original LinkedList. That way you
don't have to implement any of the sorting yourself.

To sort against a particular property, use an anonymous method to
specify a Comparison<NetworkObject>.

Thanks for your advice. I did this in the end:

public static void sortLinkedList(LinkedList<NetworkObject>
list)
{
System.Collections.ArrayList al = new
System.Collections.ArrayList(list.Count);

foreach (NetworkObject i in list)
al.Add(i);

al.Sort();

list.Clear();

foreach (NetworkObject i in al)
list.AddLast(i);

}
 
Philluminati said:
Thanks for your advice. I did this in the end:

public static void sortLinkedList(LinkedList<NetworkObject>
list)
{
System.Collections.ArrayList al = new
System.Collections.ArrayList(list.Count);

foreach (NetworkObject i in list)
al.Add(i);

al.Sort();

list.Clear();

foreach (NetworkObject i in al)
list.AddLast(i);

}

It can be simpler than that:

public static void SortLinkedList (LinkedList<NetworkObject> list)
{
List<NetworkObject> tmp = new List<NetworkObject>(list);
tmp.Sort();

list.Clear();
foreach (NetworkObject x in tmp)
{
list.Add(x);
}
}

That keeps it typesafe as well :)
 
Back
Top