By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,153 Members | 990 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,153 IT Pros & Developers. It's quick & easy.

multiple sort criterias for any class; arraylist performance

P: n/a
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
Nov 22 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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.

"herbert" wrote:
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

Nov 22 '05 #2

P: n/a
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.

"herbert" wrote:
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

Nov 22 '05 #3

P: n/a
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

Nov 22 '05 #4

P: n/a
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

Nov 22 '05 #5

P: n/a
herbert wrote:
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 :)

--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Nov 22 '05 #6

P: n/a
herbert wrote:
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 :)

--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Nov 22 '05 #7

P: n/a
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!
Nov 22 '05 #8

P: n/a
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!
Nov 22 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.