0

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
  • What will your parameter do? How will it affect the sorting algorithm? Commented Jun 15, 2010 at 19:02
  • I need to do a global and local sort. The global sort will give me a list of objects in descending priority of importace. From that I'll take the first 64 and sort them in order of Y value, with me passing in the Y value. I'll need to sor this list for 240 y values (0-239) Commented Jun 15, 2010 at 19:17

7 Answers 7

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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.
Yea, OP is very not specific. So my remark rather for him that you :)
@abatishchev - Gotcha. Sorry 'bout that.
2

You should try

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

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

Comments

1

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)));

2 Comments

what do the rhs lhs values pertain to? And I looked, vb2008 supports lambda expressions, which I never heard of :p
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.
1

Take a look at IComparer and IComparable

Comments

0

declaration (as an extension method):

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

usage:

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

Comments

0

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.