473,385 Members | 1,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

TIA
Mar 23 '07 #1
5 1119
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" <dr********@xbox-360.co.zawrote in message
news:u%****************@TK2MSFTNGP04.phx.gbl...
>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

Mar 23 '07 #2
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:
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" <dr********@xbox-360.co.zawrote in message
news:u%****************@TK2MSFTNGP04.phx.gbl...
>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öran Andersson
_____
http://www.guffa.com
Mar 23 '07 #3
thanks for your response,
i'll give it a bash
"Göran Andersson" <gu***@guffa.comwrote in message
news:OG**************@TK2MSFTNGP02.phx.gbl...
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:
>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" <dr********@xbox-360.co.zawrote in message
news:u%****************@TK2MSFTNGP04.phx.gbl...
>>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öran Andersson
_____
http://www.guffa.com

Mar 23 '07 #4
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" <dr********@xbox-360.co.zawrote in message
news:u%****************@TK2MSFTNGP04.phx.gbl...
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


Mar 23 '07 #5
That's also very helpfull.

Thank you very much!

"Milosz Skalecki [MCAD]" <mi*****@DONTLIKESPAMwp.plwrote in message
news:4B**********************************@microsof t.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:
>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" <dr********@xbox-360.co.zawrote in message
news:u%****************@TK2MSFTNGP04.phx.gbl...
>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



Mar 23 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Matthias Käppler | last post by:
Hi, I have the following problem: I want to store file objects in a container 8for now, it's not important how they look like). Now, on the one hand I need to be able to randomly pick files...
1
by: Wiseguy | last post by:
Hi. I'm having a bit of trouble overriding operators necessary to make my classes work in sorted STL containers. I'm getting a most obnoxious compiler error about negated qualifiers. Can...
3
by: Sun | last post by:
Hi I had a sortable datagrid and it worked fine when I clicked the headers. Then I introduced 2 textboxes and a button, so I can pull data based on user input. The data came out fine but the...
2
by: arjenmeijer | last post by:
I am using the Drag & Drop Sortable Lists with javaScript and CSS from http://tool-man.org/examples/sorting.html. It works beautifully in a single page, see http://www.molca.com/kkoke.html. ...
1
by: Henri Schomäcker | last post by:
Hi folks, what I need is a kind of 2D-Array which should, in the end, represent a typical html-table and which should be sortable by column. I thought of an implementation, where an vector...
3
by: TPhelps | last post by:
I have a sample of an unbound (autogeneratecolumns is true) sortable/pagable datagrid that works. I want to change one of the columns to a hyperlink. The examples I find use a bound column. I...
1
by: vincemoon | last post by:
Sites such as download.com, dealtime.com, shopping.com, and tucows.com, feature impressive sortable lists of products. What software that imports CSV can be used to generate database-driven...
20
by: Peter | last post by:
I have the following Class, but I would like to make one of the columns in my DataGrid editable, how would I do that? When I use this class none of the columns are editable even when I set the...
7
by: shellon | last post by:
Hi all: I want to convert the float number to sortable integer, like the function float2rawInt() in java, but I don't know the internal expression of float, appreciate your help!
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.