473,513 Members | 2,581 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Similar topics

0
1642
by: David Elliott | last post by:
I am looking to use a field attribute in a to find and use a property. If given Find("Record Artist", "Billy Joel"); I am looking to find the first instance in the collection where the value the...
5
1761
by: Eric Lilja | last post by:
Hello, consider this complete program (sorry, it's not minimal but I hope it's readable at least): #include <algorithm> #include <iostream> #include <vector> class Row { public:
4
2505
by: Robert Zurer | last post by:
Assuming that I have created a strongly typed collection and overridden the appropriate methods, i.e. this, Add, Insert etc., so that a sort order is maintained, it's still very possible for a...
20
4408
by: William Stacey [MVP] | last post by:
int list = {1,2,3,4,5,6}; Function to randomize the list? Cheers! -- William Stacey, MVP
5
3766
by: Learner | last post by:
Hello, Here is the code snippet I got strucked at. I am unable to convert the below line of code to its equavalent vb.net code. could some one please help me with this? static public...
7
2100
by: bonk | last post by:
Ist there a collection that holds its data sorted (i.e. inserts items sorted) AND allows to have duplicate values (by wich is sorted) ? I would like to store elements in that collection wich should...
5
3824
by: David Longnecker | last post by:
I'm working to create a base framework for our organization for web and client-side applications. The framework interfaces with several of our systems and provides the business and data layer...
1
2101
by: tshad | last post by:
I have the following code: *************************************** public class Test { .... public static TestCollection MyNewCollection() { } }
2
3202
by: =?Utf-8?B?anAybXNmdA==?= | last post by:
I'm pulling records from or database by dates, and there are multiple records for each part number. The part numbers are being stored as strings in a List in a custom Employee class: ...
0
7260
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
7160
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7525
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...
0
5685
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5086
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4746
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3233
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
799
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
456
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.