multiple sort criterias for any class; arraylist performance

G

Guest

1) I created a class Person whose objects are stored in collections.
Using IComparable allows to define one sort criteria like name.
Best practice: How can I add more sort criterias like age, zip, ...?
I don't care whether the original collection is sorted or I get a copied
collection sorted.

2) VB.NET: When is an ArrayList faster than static arrays which grow using
Redim now and then?

thanks herbert
 
G

Guest

1) You might create a class that generically sorts by using reflection.

class GenericSorter : IComparer
{
string mFieldName;
public GenericSorter(string fieldName)
{
mFieldName = fieldName;
}

public int Compare(object x, object y)
{
//Homework assignment
//Use reflection to compare object x to object y using the mFieldName
field
}
}

usage:

GenericSorter sorter = new GenericSorter("Zip");


2) You'll have to use some test code to find out for sure but I think the
difference is neglible.
 
G

Guest

Sorry Jorge, I don't understand you. For me reflection is to read type info
from assemblies.

I write in VB.NET. My class looks like this:
Class Person
Dim strName as String
Dim intAge As Integer
End Class

I create objects of this class and add them to an ArrayList. Then I want to
sort the ArrayList by Name and/or Age.
I included a CompareTo() Method implementing IComparable for default sort -
it works.

However I do not understand the online help which might tell me I should
implement IComparer too. However the IComparer Interface is implemented
outside the Person class which confuses me...

Can somebody pls give me the complete code to sort Persons by different
criteria?

thanks herbert
 
H

Helge Jensen

herbert said:
1) I created a class Person whose objects are stored in collections.
Using IComparable allows to define one sort criteria like name.
Best practice: How can I add more sort criterias like age, zip, ...?
I don't care whether the original collection is sorted or I get a copied
collection sorted.

Write external IComparer implementations, and use Array.Sort to sort:

public class AgeComparer: IComparer {
public static AgeComparer Global = new AgeComparer;
public int Compare(object x, object y) {
return ((Person)x).Age - ((Person)y).Age;
}
}
Person[] persons = ...;
System.Array.Sort(persons, AgeComparer.Global);

or make a dictionary using sorted-list:

class AgeSorted: SortedList {
public AgeSorted(int count): base(AgeComparer.Global, count) {}
}
IDictionary ages = new AgeSorted();
...
foreach ( Person p in ages.Keys ) /* in age order */
f(p);

Note that SortedList seems to be pretty slow when items are inserted
out-of-order, possibly because it's based on an Array. If that's a
problem for you there is really no remedy except implementing you own
sorted data-structure using a tree.
2) VB.NET: When is an ArrayList faster than static arrays which grow using
Redim now and then?

ArrayList would probably be faster all the time... try making a test :)
 
G

Guest

Thanks Helge,
the code works and in VB.NET it's as short as this:

Public Class AgeComparer
Implements IComparer
Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer
_ Implements System.Collections.IComparer.Compare
Return CType(x, ClsPerson).Age - CType(y, ClsPerson).Age
End Function

Usage:
'sort by age
Dim myComparer As New AgeComparer
myAL.Sort(myComparer)

thanks!
 

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