User class sorting

W

Wing Siu

Dear All

I would like to develop a generic class sorting function.
Currently, when I need sort a collection of user class, for example: Person,
Exhibition, Country

I need implement a comparer class for each user class, for example:
PersonComparer, ExhibitionComparer, CountryComparer

Is that any solution that allow me sort different class by using the same
comparer and use different attribute of the user class?

for exhibition
For my idea something like follow
void VisualComparer(Collection items, TypeOf class, string sortProperty)

When I call this function
VisualComparer(Persons, typeOf(Person), "name");

Then this function will basic on Person.Name property to perform sorting

Thanks
 
L

Lebesgue

This should be accomplished by implementing an IComparer interface and
passing it to the Sort method of your collections.
If you want the class implementing IComparer to be generic (e.g. able to
compare any class based on property name) you should implement it as
follows:

class Comparer<T> : IComparer<T>
{
private PropertyInfo property;

public Comparer(string propertyName)
{
PropertyInfo i = typeof(T).GetProperty(propertyName);

//the property you want to sort by must be comparable (strings, ints and
other primitive datatypes are)
if (i.PropertyType.FindInterfaces(new TypeFilter(delegate(Type t, object
o)
{
return t == typeof(IComparable);
}), null).Length == 0)
{
throw new ArgumentException("Specified property is not comparable");
}

this.property = i;
}

public int Compare(T x, T y)
{
IComparable cx = property.GetValue(x, null) as IComparable;
IComparable cy = property.GetValue(y, null) as IComparable;

return cx.CompareTo(cy);
}
}

//use it like this
List<Person> l = new List<Person>();
l.Add(new Person("a"));
l.Add(new Person("c"));
l.Add(new Person("b"));
l.Add(new Person("a"));
l.Sort(new Comparer<Person>("Name"));

Hope this helps.
 
W

Wing Siu

Hi Lebesgue

Thanks your quick reply
What's meaning of Comparer<T>
I haven't see this format before, would you mind teach me what it is?

If I have an ArrayList called persons
should I use the class like follow
persons.Sort(new Comparer<Person>("Name"));

Thanks
 
L

Lebesgue

The meaning of <T> in Comparer class is that it's a generic type parameter.
Generics are new to .NET 2.0.
You can learn about them here:
http://msdn2.microsoft.com/en-us/library/512aeb7t.aspx
If you are not using .NET 2.0, the code can be modified to use System.Type
as parameter instead of using generics.
Let me know if you can handle it or should I modify the code for you.
 
W

Wing Siu

wor~ Lebesgue

Thanks for your help, you help me a lot and I learnt more from your url.
Thanks
^.^
 
W

Wing Siu

Hi Machin

What's the purpose of following code:
if ( comparer.IndexOf( ".") != -1 ) { }

Why it check the comparer has "." or not?
Would you please provide the sample argument value for this parameter?

Thanks
 
J

John B

Wing said:
Hi Machin

What's the purpose of following code:
if ( comparer.IndexOf( ".") != -1 ) { }

Why it check the comparer has "." or not?
Would you please provide the sample argument value for this parameter?
 
J

John B

Wing said:
Hi Machin

What's the purpose of following code:
if ( comparer.IndexOf( ".") != -1 ) { }

Why it check the comparer has "." or not?
Would you please provide the sample argument value for this parameter?
<...>

It would appear that that is to cater for sub-properties.
So if you wanted to sort a customer class by Address.State you could
define the comparer as "Address.State" and it would get the address
property then recursively call Compare and pass the address value and
"State" as the compareproperty.

I do the same thing but cater for more than one level so you could pass
"Customer.Address.State" and it would do a join to recursively sort by
"Address.State" after it retrieved the customer property.

JB
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

John B is correct, you could sort by " Address.State" if you want.

also notice that you could extend it to use the return of a method as an
organizing criteria.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top