Question on Collection Sorting

L

Lee

When extending CollectionBase is it possible to sort on a particular
field of each object in the collection? For example if I created a
inherited class from CollectionBase and filled it with "Sale" objects
and each Sale object had a SaleID property, can I sort by that?

Thanks,

--
Warm Regards,
Lee

"Upon further investigation it appears that your software is missing
just one thing. It definitely needs more cow bell..."
 
M

Mike

You have to create your own Sort method.

I typically do the following:
1) make the collection class implement IComparer which means I have to
create a method of this signature:
public int Compare(object x, object y) { ...}
2) add an child enum of the class called SortOptions adding each
property I want to be able to sort on
3) add methods to add or clear items from the sort priority.
4) implement sort to call "this.InnerList.Sort(this);" This passes the
current class into the base class sort method, so the results of the
Compare method in step one determine the order of items in the
collection.

This example shows how to make your collection sortable on multiple
property values at the same time. It could be more simple if you only
needed to sort on one property at a time. Just replace the sort option
list, with a single sort option property.

public class PersonCollection:CollectionBase, IComparer
{
private ArrayList sortOptions_ = new ArrayList();
public enum SortOptions
{
FullNameAsc, FullNameDesc, JobTitleAsc, JobTitleDesc
}
public void AddSort(SortOptions option)
{
this.sortOptions_.Add(option);
}
public void ClearSort()
{
this.sortOptions_.Clear();
}
public void Sort()
{
if (this.sortOptions_.Count == 0)
return;

this.InnerList.Sort(this);
}
public int Compare(object x, object y)
{
int retVal = 0;
Person px = (Person)x;
Person py = (Person)y;

for (int i=0; i < this.sortOptions_.Count; i++)
{
SortOptions option = (SortOptions)this.sortOptions_;
switch(option)
{
case SortOptions.FullNameAsc:
retVal = px.FullName.CompareTo(py.FullName);
break;
case SortOptions.FullNameDesc:
retVal = py.FullName.CompareTo(px.FullName);
break;
case SortOptions.JobTitleAsc:
retVal =
px.Title.FamilyDescription.CompareTo(py.Title.FamilyDescription);
break;
case SortOptions.JobTitleDesc:
retVal =
py.Title.FamilyDescription.CompareTo(px.Title.FamilyDescription);
break;
}
//if not equal after the current sortoption, then done
// comparing. (Stop at the first sortoption where differ)
if (retVal != 0)
break; // from sort option loop
}
return retVal;
}
}
 
L

Lee

Mike enlightened me by writing:
You have to create your own Sort method.

I typically do the following:
1) make the collection class implement IComparer which means I have to
create a method of this signature:
public int Compare(object x, object y) { ...}
2) add an child enum of the class called SortOptions adding each
property I want to be able to sort on
3) add methods to add or clear items from the sort priority.
4) implement sort to call "this.InnerList.Sort(this);" This passes
the current class into the base class sort method, so the results of
the Compare method in step one determine the order of items in the
collection.

Excellent example. Thank you for taking the time.



--
Warm Regards,
Lee

"Upon further investigation it appears that your software is missing
just one thing. It definitely needs more cow bell..."
 
L

Lee

Mike enlightened me by writing:

public class PersonCollection:CollectionBase, IComparer
{
private ArrayList sortOptions_ = new ArrayList();
public enum SortOptions
{


I'm just learning C# syntax. What does the trailing underscore
signify? Is that just a convention?

--
Warm Regards,
Lee

"Upon further investigation it appears that your software is missing
just one thing. It definitely needs more cow bell..."
 
M

Mike

It is just a naming convention I use. I prefix field names with
underscore, and use camel casing. I use Pascal casing on public
members. I name the private field the same as a corresponding public
member, except for the case and _ difference.

Mike
 
M

Mike

excuse the confusion above. I meant to say I suffix the field name.
however, prefixing would be just as useful. I've been in places that
require an underscore prefix on field names.

I also use the same name for method parameters and the private field
they may relate to, except that a method parameter does not contain the
underscore.

An underscore is not required or meaningful to the C# compiler.

Mike
 

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