herbert wrote:[color=blue]
> 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.[/color]
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.
[color=blue]
> 2) VB.NET: When is an ArrayList faster than static arrays which grow using
> Redim now and then?[/color]
ArrayList would probably be faster all the time... try making a test :)
--
Helge Jensen
mailto:helge.jensen@slog.dk
sip:helge.jensen@slog.dk
-=> Sebastian cover-music:
http://ungdomshus.nu <=-