473,396 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

multiple sort criterias for any class; arraylist performance

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
8 5033
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: herbert | last post by:
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,...
19
by: Derek Martin | last post by:
Hi there, I have been playing with sorting my arraylist and having some troubles. Maybe just going about it wrong. My arraylist contains objects and one of the members of the object is 'name.' I...
48
by: Alex Chudnovsky | last post by:
I have come across with what appears to be a significant performance bug in ..NET 2.0 ArrayList.Sort method when compared with Array.Sort on the same data. Same data on the same CPU gets sorted a...
11
by: John | last post by:
Hi All, Although C# has Generics, it still does not support the generic programming paradigm. Multiple inheritance is required to support real generic programming. Here is a simple design pattern...
25
by: Darsin | last post by:
Hi all I need to perform a summation on a column of a table based on a criteria given in another column in the same table. The catch is i need to perform different sums according to the number of...
3
by: raylopez99 | last post by:
This is an example of using multiple comparison criteria for IComparer/ Compare/CompareTo for List<and Array. Adapted from David Hayden's tutorial found on the net, but he used ArrayList so the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.