make a class sortable

Discussion in 'ASP .Net' started by Grant Merwitz, Mar 23, 2007.

  1. 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
     
    Grant Merwitz, Mar 23, 2007
    #1
    1. Advertising

  2. 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" <> wrote in message
    news:u%...
    >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
    >
     
    Grant Merwitz, Mar 23, 2007
    #2
    1. Advertising

  3. 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" <> wrote in message
    > news:u%...
    >> 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
     
    =?ISO-8859-1?Q?G=F6ran_Andersson?=, Mar 23, 2007
    #3
  4. thanks for your response,
    i'll give it a bash


    "Göran Andersson" <> wrote in message
    news:...
    > 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" <> wrote in message
    >> news:u%...
    >>> 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
     
    Grant Merwitz, Mar 23, 2007
    #4
  5. 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" <> wrote in message
    > news:u%...
    > >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
    > >

    >
    >
    >
     
    =?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=, Mar 23, 2007
    #5
  6. That's also very helpfull.

    Thank you very much!

    "Milosz Skalecki [MCAD]" <> wrote in message
    news:...
    > 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" <> wrote in message
    >> news:u%...
    >> >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
    >> >

    >>
    >>
    >>
     
    Grant Merwitz, Mar 23, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?RGlmZmlkZW50?=

    Setting CSSClass for Datagrid header's for sortable fields

    =?Utf-8?B?RGlmZmlkZW50?=, Dec 22, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    1,616
    =?Utf-8?B?QmlsbCBCb3Jn?=
    Dec 23, 2004
  2. Replies:
    0
    Views:
    2,467
  3. =?Utf-8?B?VFBoZWxwcw==?=

    Sortable/pagable datagrid with hyperlink column

    =?Utf-8?B?VFBoZWxwcw==?=, Dec 29, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    4,736
    Steven Cheng[MSFT]
    Jan 3, 2006
  4. tonicvodka
    Replies:
    0
    Views:
    349
    tonicvodka
    Jan 18, 2006
  5. Wiseguy
    Replies:
    1
    Views:
    400
    Jerry Coffin
    Mar 13, 2005
Loading...

Share This Page