Friday, March 16, 2012

make a class sortable

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

TIAOk, 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@.xbox-360.co.zawrote in message
news:u%23DakFTbHHA.2316@.TK2MSFTNGP04.phx.gbl...

Quote:

Originally Posted by

>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
>


Make a custom comparer class (implementing IComparer or IComparer<>)
that contains a property for ascending/descending and a property that
decides what property to sort on.

Create an object from the class, set the properties, and use the object
in the call to Array.Sort.

Grant Merwitz wrote:

Quote:

Originally Posted by

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@.xbox-360.co.zawrote in message
news:u%23DakFTbHHA.2316@.TK2MSFTNGP04.phx.gbl...

Quote:

Originally Posted by

>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
>>


>


--
Gran Andersson
_____
http://www.guffa.com
thanks for your response,
i'll give it a bash

"Gran Andersson" <guffa@.guffa.comwrote in message
news:OGLHCmUbHHA.4176@.TK2MSFTNGP02.phx.gbl...

Quote:

Originally Posted by

Make a custom comparer class (implementing IComparer or IComparer<>) that
contains a property for ascending/descending and a property that decides
what property to sort on.
>
Create an object from the class, set the properties, and use the object in
the call to Array.Sort.
>
Grant Merwitz wrote:

Quote:

Originally Posted by

>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@.xbox-360.co.zawrote in message
>news:u%23DakFTbHHA.2316@.TK2MSFTNGP04.phx.gbl...

Quote:

Originally Posted by

>>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
>>>


>>


>
>
--
Gran Andersson
_____
http://www.guffa.com


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:

Quote:

Originally Posted by

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@.xbox-360.co.zawrote in message
news:u%23DakFTbHHA.2316@.TK2MSFTNGP04.phx.gbl...

Quote:

Originally Posted by

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


>
>
>


That's also very helpfull.

Thank you very much!

"Milosz Skalecki [MCAD]" <mily242@.DONTLIKESPAMwp.plwrote in message
news:4B41BF90-49D1-4F38-9412-DC6ACF445347@.microsoft.com...

Quote:

Originally Posted by

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:
>

Quote:

Originally Posted by

>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@.xbox-360.co.zawrote in message
>news:u%23DakFTbHHA.2316@.TK2MSFTNGP04.phx.gbl...

Quote:

Originally Posted by

>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
>


>>
>>
>>

0 comments:

Post a Comment