SortedBindingList question

SortedBindingList question

Old forum URL: forums.lhotka.net/forums/t/315.aspx


Massong posted on Thursday, June 08, 2006

Hi Rocky, hi all,

 

I need to sort a collection by more than one property – in one case by 11 properties to find items that I can group together. At the moment I can help myself with a List(Of T). Will it be possible to sort a future version of the SortedBindingList like an ArrayList with a specified comparer implementing IComparer?

 

In another scenario the user can select an item of a collection to move it up or down for one position. I would prefer to set a protected SortIndex in the Update-method of the collection instead of in the UI. In my CSLA 1.x implementation I added a MoveUp and MoveDown method to the collection and manipulated the InnerList with RemoveAt and Insert. How can I implement this scenario with CSLA 2.0?

 

Thanks for your support,
Christian

SonOfPirate replied on Friday, June 09, 2006

First, here's another thread where this subject has already been discussed:

http://forums.lhotka.net/forums/thread/899.aspx

To address your specific question about the IComparer interface, yes there is a way to do it within the CSLA construct.  The way we handled this was to overload the ApplySort methods (we have several) and add "IComparer comparer" as an additional parameter.  You can either store that as a private variable or pass it to the DoSort method as an argument.

Then, if you look closely at the code for DoSort, you will see that sorting is actually deferred to the _sortIndex list.  The Sort method for a System.Collection.Generic.List<T> object has overloads that will do what you want.  In our case we also implemented range sorting using "index" and "count" arguments as well as allowing developers to use their own IComparer objects for sorting.  All of these can be passed into the _sortIndex.Sort() methods within the DoSort routine without any additional changes to the code or logic, such as:

_sortIndex.Sort(comparer);

You don't need to check for null because the underlying List control handles that for you.

Hope that helps.

 

newbie replied on Friday, June 16, 2006

Nevermind. I figured out the problem.

Thanks anyway.

Here is how I did to make it work for me. It may work for you for multiple properties sorting...

I updated CompareTo method in private class ListItem : IComparable<ListItem> class shown in red. That is the ONLY change I made to CSLA 2.0 solution.

public int CompareTo(ListItem other)

{

object target = other.Key;

//made IComparable a generic type

if (Key is IComparable<T>)

return ((IComparable<T>)Key).CompareTo((T)target);

...

...}

Then implemented CompareTo method for IComparable interface in the class which was inheriting from Csla.BusinessBase.

eg. I am sorting Form object by LineOfBusinessTypeiD, FormPrefix, and then FormNumber

public partial class Form : Csla.BusinessBase<Form>, IComparable<Form>

#region IComparable Members

public int CompareTo(Form y)

{

//compare by LOB Type first

//coalesce to non-null values for comparison

int xLOB = (int?)this.LineOfBusinessTypeID ?? -1;

int yLOB = (int?)y.LineOfBusinessTypeID ?? -1;

if (xLOB < yLOB)

{

return -1;

}

else if (xLOB > yLOB)

{

return 1;

}

else

{

//sort by form Prefix

if ((this.FormPrefix.CompareTo(y.FormPrefix)) < 0)

{

return -1;

}

else if ((this.FormPrefix.CompareTo(y.FormPrefix)) > 0)

{

return 1;

}

else

{

//sort by form Number

if ((this.FormNumber.CompareTo(y.FormNumber)) < 0)

{

return -1;

}

else if ((this.FormNumber.CompareTo(y.FormNumber)) > 0)

{

return 1;

}

else

{

return 0;

}

}

}

}

#endregion

To use this,

SortedBindingList<Form> sortedList = new SortedBindingList<Form>(TempformsList);

//calls Form.CompareTo IComparable method which sorts by LOB, FormPrefix, then FormNumber. Keep PropertyName field blank (1st parameter in ApplySort).

sortedList.ApplySort("", System.ComponentModel.ListSortDirection.Ascending);

foreach (Form form in sortedList)//add sorted forms to formsList

formsList.Add(form);

Finally, formsList will contain a sorted list of forms by LineOfBusinessTypeID, FormPrefix and FormNumber.

Aztec_2Step replied on Thursday, March 20, 2008

The problem I have with this solution is that it makes modifications to the SortedBindingList class where you are making the framework aware of your Subclass T. This may be defined in your implementation of CSLA or by making CSLA dependent on another assembly that has T defined. Also it is kind of embedding a one-off solution in a more general framework.

Don't get me wrong, I'm a newbie to C# and CSLA. I'm not trying to tell everyone their business but this is what strikes me as a possible problem...

I would prefer using the ApplySort(IComparer comparer) method but I've not seen any code on it. If anyone could email me such code, I'd appreciate it greatly!!! Aztec_2Step ( @ ) hotmail.com

Copyright (c) Marimer LLC