System.Array.Sort on a Class Array

  • Thread starter Thread starter ShrimpBoy
  • Start date Start date
S

ShrimpBoy

Hi!

I have an array filled with a class that have some properties...

I noticed the Sort method but I don't know how to use it to sort my items...

I have an array filled with object of a class like this...

Person.FirstName
Person.LastName
Person.Special
Person.Comment

I want to sort them by their FirstName...

Is their a way to do it?

Thanks a lot!

Fred
 
Hello,

you can either implement IComparable on your Person object, or call the Sort
method which takes either an IComparer or the Comparison delegate.

In either of those scenarios you are dealing with two instances of type
Person where you can compare them according to your rules (e.g. Firstname).

Kind regards,
Henning
 
Hi!

I looked at System.Collections.IComparer

It has a Compare method but I really don't know how to use it to do what I
need...

Dim Comparer As System.Collections.IComparer
Comparer(Person(1).FirstName, Person(2).FirstName)
System.Array.Sort(Person, Comparer)

It means I have to have 2 items or more in my array, I'm really not sure how
to use it..:S

Can you help me?

Thanks!

Fred
 
Back
Top