make a class sortable

G

Grant Merwitz

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
 
G

Grant Merwitz

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
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

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

Grant Merwitz

thanks for your response,
i'll give it a bash


Göran Andersson said:
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 said:
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 said:
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
 
G

Guest

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

Grant Merwitz

That's also very helpfull.

Thank you very much!

Milosz Skalecki said:
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 said:
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 said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top