Let's say I have a custom object of Foo

Is there anyway I can sort through a list of these objects, like

list<of foo>.sort()

and also be able to sort this list with a passable parameter. which will influence the sort?

list<of foo>.sort(pValue)

I'm guessing I'll need to define two separate sorts, but I am not sure.

EDIT: using built-in list. How objects are sorted depends on whether or not a parameter is passed. I could use a global variable and use that for sorting, but that makes bad bac code :p

+2  A: 

If you're the one defining the Sort() method, then yes.

It's easy enough to overload the Sort() method in C# (or using an optional parameter in VB.NET).

If you're talking about List.Sort() that is baked into the framework...then I'm not sure what you want to do and you'll need to provide some more details.

Justin Niessner
To override `Sort()` you have to define your own `List`, i.e. `FooList : List<T> where T : Foo { }`
@abatishchev - Exactly. He's not specific so I can't tell if he's using the List<T> collection from the framework or not.
Justin Niessner
@Justin: Yea, OP is very not specific. So my remark rather for him that you :)
@abatishchev - Gotcha. Sorry 'bout that.
Justin Niessner

declaration (as an extension method):

public static void Sort(this List<Foo> list, Bar param)
   // your sorting algorithm


var list = new List<Foo>();
list.Add(new Foo());
list.Sort(); // standard algorithm
list.Sort(new Bar()); // your own algorithm
+1  A: 

Take a look at IComparer and IComparable

+1  A: 

This is fairly easy in C#. Just create lambda functions for both, and you can specify the parameter right in the lambda function. I know VB supports extension methods, but I'm not sure about lambda functions.

// Sorting ints based on natural order:
List<Int32> ints = GetInts();
ints.Sort((lhs, rhs) => lhs.Compare(rhs));

// Sorting Foo objects based on some computation:
List<Foo> foos = GetFoos();
foos.Sort((lhs, rhs) => lhs.Compute(pValue).Compare(rhs.Compute(pValue)));
what do the rhs lhs values pertain to? And I looked, vb2008 supports lambda expressions, which I never heard of :p
Jeffrey Kern
They are simply the first and second parameter. You are specifying the ordering function, which takes two parameters of the type of the `IEnumerable`. I simply name them `lhs` and `rhs` to make it easy for me to remember their positioning. The return is an `Int32`, just like the `IComparable.Compare` method. So if `lhs` should come first, the return value should be negative.

You have three choices:

  • Your class being sorted can implement IComparable
  • You can implement an IComparer<of foo> and use an instance of it when calling sort().
  • You can create a function that compares two foo objects and use that function when calling sort().

The advantage of the comparer is you can parametrize it more easily since you create an instance and can set any property in it prior to use. The disadvantage is you have to create a separate class.

The delegate is simpler to use but a little less clean if you want to parametrize the compare.

+1  A: 

You should try

list<of foo>.OrderBy( .... )

where inside OrderBy you can use lamda expressions to control your sort algorithm by parameters and the order of sort

Akash Kava

As has already been stated, you should have a look at implementing an IComparable interface on your object. You can then create a new class implementing IComparer which will be used to control the sorting you want to apply. There are a couple of good articles here and here that should get you started.

Matt Weldon