469,129 Members | 1,720 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,129 developers. It's quick & easy.

Trying to simplify my List Collection Sort

I have dataGrid that I am filling from a List Collection and need to sort it
by the various columns.

So I need to be able to sort the Collection and found that you have to set
up your own sorting functions to make it work. I have build the following 3
sorting
functions for the 3 columns and have to call each one specifically.

I am trying to find a way to cut simplify the functions and calls to make
this a more
general class. At the moment, if I add another column I need to add another
sorting method
and another call from my switch.

Here is the Class and Sorts
**********************************************
using System;
using System.Collections.Generic;

namespace Irez
{
[Serializable()]
public class DisplayAmount : IComparable<DisplayAmount>
{
public DisplayAmount()
{
}
string mstrDisplayName = "";
double mdblDisplayValue = 0;
string mstrTransactionDescription = "";

public string DisplayName
{
get{ return mstrDisplayName; }
set{ mstrDisplayName = value;}
}

public double DisplayValue
{
get{ return mdblDisplayValue;}
set{ mdblDisplayValue = value;}
}

public string TransactionDescription
{
get{ return mstrTransactionDescription;}
set{ mstrTransactionDescription = value;}
}

// Sorts
public static int CompareName(DisplayAmount c1, DisplayAmount c2)
{
return c1.DisplayName.CompareTo(c2.DisplayName);
}

public static int CompareValue(DisplayAmount c1, DisplayAmount c2)
{
return c1.DisplayValue.CompareTo(c2.DisplayValue);
}

public static int CompareTransactionDescription(DisplayAmount c1,
DisplayAmount c2)
{
return
c1.TransactionDescription.CompareTo(c2.Transaction Description);
}
}

// Define Collections here
public class DisplayAmountCollection : List<DisplayAmount{ }
}
************************************************** **********

To sort the collection I call the following function with the Collection,
the name of the column to sort and
the direction.

************************************************** ***********
protected void SortList(ref DisplayAmountCollection ocol, string
sortExpression, string sortDirection)
{
Comparison<DisplayAmountoColDel = null;

switch (sortExpression)
{
case "DisplayName":
oColDel = new
Comparison<DisplayAmount>(DisplayAmount.CompareNam e);
ocol.Sort(oColDel);
break;
case "DisplayValue":
oColDel = new
Comparison<DisplayAmount>(DisplayAmount.CompareVal ue);
ocol.Sort(oColDel);
break;
case "TransactionDescription":
oColDel = new
Comparison<DisplayAmount>(DisplayAmount.CompareTra nsactionDescription);
ocol.Sort(oColDel);
break;
}
if (sortDirection == "DESC") ocol.Reverse();
}
************************************************** ***********

I'd like to see if I can simplify this if possible. If I have 10 columns, I
would have 10 case statements.

Also, I was trying to do a similar thing with my "Comparison<DisplayAmount>"
as I do with my "DisplayListCollection".

I don't have to do:

List<DisplayAmountd1 = null;

in my code. I put this in my class:

public class DisplayAmountCollection : List<DisplayAmount{ }

and then call it in my program as:

DisplayAmountCollection dc = null;

But I can't seem to the same with my Comparison<tstatement without getting
an error. If I do the following:

public class DisplayAmountSort : Comparison<DisplayAmount{ }

I get the error:

cannot derive from sealed type 'System.Comparison<DisplayAmount>'

Why can I do it with List but not with Comparison?

Thanks,

Tom
Jan 16 '08 #1
3 3957
Hi,
If you are always going to sort by using only one column you can use a code
I created a time ago just for this:
http://groups.google.com/group/micro...97990b9dcd65e2

The good part about it is that the "sorted" class do not have knowledge of
it. you can sort a collection of ANY type.

You might ahve to convert it to use IComparer<Tas the code was created in
1.1

--
Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.
"tshad" <tf*@dslextreme.comwrote in message
news:uY**************@TK2MSFTNGP04.phx.gbl...
>I have dataGrid that I am filling from a List Collection and need to sort
it
by the various columns.

So I need to be able to sort the Collection and found that you have to set
up your own sorting functions to make it work. I have build the following
3 sorting
functions for the 3 columns and have to call each one specifically.

I am trying to find a way to cut simplify the functions and calls to make
this a more
general class. At the moment, if I add another column I need to add
another sorting method
and another call from my switch.

Here is the Class and Sorts
**********************************************
using System;
using System.Collections.Generic;

namespace Irez
{
[Serializable()]
public class DisplayAmount : IComparable<DisplayAmount>
{
public DisplayAmount()
{
}
string mstrDisplayName = "";
double mdblDisplayValue = 0;
string mstrTransactionDescription = "";

public string DisplayName
{
get{ return mstrDisplayName; }
set{ mstrDisplayName = value;}
}

public double DisplayValue
{
get{ return mdblDisplayValue;}
set{ mdblDisplayValue = value;}
}

public string TransactionDescription
{
get{ return mstrTransactionDescription;}
set{ mstrTransactionDescription = value;}
}

// Sorts
public static int CompareName(DisplayAmount c1, DisplayAmount c2)
{
return c1.DisplayName.CompareTo(c2.DisplayName);
}

public static int CompareValue(DisplayAmount c1, DisplayAmount c2)
{
return c1.DisplayValue.CompareTo(c2.DisplayValue);
}

public static int CompareTransactionDescription(DisplayAmount c1,
DisplayAmount c2)
{
return
c1.TransactionDescription.CompareTo(c2.Transaction Description);
}
}

// Define Collections here
public class DisplayAmountCollection : List<DisplayAmount{ }
}
************************************************** **********

To sort the collection I call the following function with the Collection,
the name of the column to sort and
the direction.

************************************************** ***********
protected void SortList(ref DisplayAmountCollection ocol, string
sortExpression, string sortDirection)
{
Comparison<DisplayAmountoColDel = null;

switch (sortExpression)
{
case "DisplayName":
oColDel = new
Comparison<DisplayAmount>(DisplayAmount.CompareNam e);
ocol.Sort(oColDel);
break;
case "DisplayValue":
oColDel = new
Comparison<DisplayAmount>(DisplayAmount.CompareVal ue);
ocol.Sort(oColDel);
break;
case "TransactionDescription":
oColDel = new
Comparison<DisplayAmount>(DisplayAmount.CompareTra nsactionDescription);
ocol.Sort(oColDel);
break;
}
if (sortDirection == "DESC") ocol.Reverse();
}
************************************************** ***********

I'd like to see if I can simplify this if possible. If I have 10 columns,
I would have 10 case statements.

Also, I was trying to do a similar thing with my
"Comparison<DisplayAmount>" as I do with my "DisplayListCollection".

I don't have to do:

List<DisplayAmountd1 = null;

in my code. I put this in my class:

public class DisplayAmountCollection : List<DisplayAmount{ }

and then call it in my program as:

DisplayAmountCollection dc = null;

But I can't seem to the same with my Comparison<tstatement without
getting an error. If I do the following:

public class DisplayAmountSort : Comparison<DisplayAmount{ }

I get the error:

cannot derive from sealed type 'System.Comparison<DisplayAmount>'

Why can I do it with List but not with Comparison?

Thanks,

Tom

Jan 16 '08 #2
But if I am passing it with "ref", doesn't that say to pass it by reference
and not value?
This gets tricky to explain... the short version is that List<T(etc)
is a reference-type, which means that even without "ref" all you are
passing is the address of the list (on the managed heap). If you use
"ref" here, then instead of passing the address of the list, it passes
the address of the *variable* in the calling code (typically on the
stack, although it could be a field etc). The only things "ref" gives
you here is the ability to update the variable directly and have that
reassignment propegate to the calling method - but you don't need to
do that. Neither way (alone) causes the list to clone itself.

Jon Skeet puts it better: http://www.pobox.com/~skeet/csharp/parameters.html

Marc
Jan 16 '08 #3
Which cited links?

http://groups.google.com/group/micro...23312d4c9ae353
(this is for a BindingList<Timplementation of IBindingList-based and
IBindingListView-based sorting, which is *exactly* what is used
automatically when you click on the column headers).

Marc
Jan 17 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Robert Zurer | last post: by
20 posts views Thread by William Stacey [MVP] | last post: by
5 posts views Thread by David Longnecker | last post: by
1 post views Thread by tshad | last post: by
2 posts views Thread by =?Utf-8?B?anAybXNmdA==?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by Mortomer39 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.