Re: make a class sortable
- From: "Grant Merwitz" <driftzilla@xxxxxxxxxxxxxx>
- Date: Fri, 23 Mar 2007 22:38:41 +0200
That's also very helpfull.
Thank you very much!
"Milosz Skalecki [MCAD]" <mily242@xxxxxxxxxxxxxxxxx> wrote in message
news:4B41BF90-49D1-4F38-9412-DC6ACF445347@xxxxxxxxxxxxxxxx
Howdy,
There are two methods. First is sorting by IComparer implementator
(supported both by .NET 1.1 and 2.0) IComparer. Second is by using
generics
(only supported only by .NET 2.0). Please find example that should help
you
with both cases:
public class MyClass
{
public MyClass(string className, string classType)
{
this.className = className;
this.classType = classType;
}
private string className;
public string ClassName
{
get
{
return this.className;
}
}
private string classType;
public string ClassType
{
get
{
return this.classType;
}
}
public static int CompareByClassNameAsc(MyClass a, MyClass b)
{
return String.Compare(a.ClassName, b.ClassName);
}
public static int CompareByClassNameDesc(MyClass a, MyClass b)
{
return -String.Compare(a.ClassName, b.ClassName);
}
public static int CompareByClassTypeAsc(MyClass a, MyClass b)
{
return String.Compare(a.ClassType, b.ClassType);
}
public static int CompareByClassTypeDesc(MyClass a, MyClass b)
{
return -String.Compare(a.ClassType, b.ClassType);
}
}
public class MyClassByClassNameComparer : System.Collections.IComparer
{
private SortDirection sortDirection;
/// <summary>
///
/// </summary>
public SortDirection SortDirection
{
get
{
return this.sortDirection;
}
set
{
this.sortDirection = value;
}
}
public int Compare(object x, object y)
{
MyClass a = (MyClass) x;
MyClass b = (MyClass) y;
int result = String.Compare(a.ClassName, b.ClassName);
if (this.SortDirection == SortDirection.Descending)
result = -result;
return result;
}
}
public class MyClassByClassTypeComparer : System.Collections.IComparer
{
private SortDirection sortDirection;
/// <summary>
///
/// </summary>
public SortDirection SortDirection
{
get
{
return this.sortDirection;
}
set
{
this.sortDirection = value;
}
}
public int Compare(object x, object y)
{
MyClass a = (MyClass) x;
MyClass b = (MyClass) y;
int result = String.Compare(a.ClassType, b.ClassType);
if (this.SortDirection == SortDirection.Descending)
result = -result;
return result;
}
}
public enum SortDirection
{
Ascending,
Descending
}
and finally the usage:
MyClass[] arr1 = new MyClass[] {
new MyClass("a", "a"),
new MyClass("b", "b"),
new MyClass("c", "c") };
MyClass[] arr2 = new MyClass[] {
new MyClass("a", "a"),
new MyClass("b", "b"),
new MyClass("c", "c") };
// first method
MyClassByClassNameComparer comp = new MyClassByClassNameComparer();
comp.SortDirection = SortDirection.Descending;
Array.Sort(arr1, comp);
// second method
Array.Sort<MyClass>(arr2, new Comparison<MyClass>(
MyClass.CompareByClassNameDesc));
--
Milosz
"Grant Merwitz" wrote:
Ok, i've found out i should use the IComparable interface.
This goes like this
public class MyClass : IComparable
{
public int CompareTo(object x)
{
MyClass myClass = (MyClass)x;
return string.Compare(ClassName, pm.ClassName);
}
}
And i utilise this using the;
Array.Sort(myClassArr);
2 questions:
1) How would i set this to ASC and DESC?
2) What is the best way to set which property to sort by?
I realise i could set a private variable within the class to
decide
this,
but there must be a better way
Again, any help would be much appreciated
"Grant Merwitz" <driftzilla@xxxxxxxxxxxxxx> wrote in message
news:u%23DakFTbHHA.2316@xxxxxxxxxxxxxxxxxxxxxxx
I have a basic object in an array.
I bind this class to a gridview.
How can i make this class sortable?
Here's an example class
public class MyClass
{
public MyClass(classname, classtype)
{
this._ClassName = classname;
this._ClassType = classtype;
}
public string ClassName
{ get { return this._ClassName; }}
public string ClassType
{ get { return this._ClassType; }}
private string _ClassName;
private stirng _ClassType;
}
I now bind this to a grid:
MyClass[] myClassArr = new MyClass[3] { new MyClass("a", "a"), new
MyClass("b", "b"), new MyClass("c", "c") };
gvMyClassGrid.DataSource = myClassArray;
gvMyClassGrid.DataBind();
On the sort command, i want to be able to sent a sort command to my
class.
Something along the lines of:
myClassArr = MyClass.SortClassArray("ClassName", "ASC");
Any advice on how i should set about achieving this would be greatly
appreciated
TIA
.
- References:
- make a class sortable
- From: Grant Merwitz
- Re: make a class sortable
- From: Grant Merwitz
- Re: make a class sortable
- From: Milosz Skalecki [MCAD]
- make a class sortable
- Prev by Date: Re: Connection String to connect to an Oracle DB in a remote server
- Next by Date: Re: Errors on ..ServerVariables
- Previous by thread: Re: make a class sortable
- Next by thread: RE: converted project won't intellisense!
- Index(es):
Relevant Pages
|