Sorting ArrayLists by Multipul Columns

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • iKiLL

    Sorting ArrayLists by Multipul Columns

    Hi All,

    My background is that i am new to C- Sharp and OOP. I have been working with
    it for about 2 Weeks. I do have good experionce in developing in things like
    VB6, COM, ASP, JavaScript and i have done some development with VB.Net but
    it was not really done in an OOP fashion. i have Been asked to take over a
    PDA application that has been writen in C-Sharp 2005.

    My Question.
    The application is holting in Memory an ArrayList of Tasks that it has
    De-Serialized from an XML data file on the PDA.

    Now I need to Sort the data buy 3 Columns (These have been setup as
    properties of the object "I Think?") just like you would in an SQL (OrderBy
    Age, Fname, Lname)

    I have been reading all day and i still can not figure out how to do this.
    Below is a Code Snippet i think might work but i reall am batteling to
    understand it and i Can not figger out how to use it to get what i want.

    If some one could please help me by just comment each line for me with and
    explenation on what each part of it means and how it is working i would be
    very greatefull.
    Even the smallest details would be most beneficial for me, and would be much
    appriciated.

    Many Thanks,
    Ink




    using System;

    using System.Collecti ons.Generic;

    using System.Text;

    using System.Collecti ons;

    using System.Reflecti on;

    namespace PPCAssetClient. Helpers

    {

    public class ArrayCompareCla ss : IComparer

    {

    private string compareby = "";

    private string[] subprops = null;

    #region IComparer Members

    public void SortBy(string compareby)

    {

    this.compareby = compareby;

    }

    public string[] SubProperties

    {

    get { return subprops; }

    set { subprops=value; }

    }

    public int Compare(object x, object y)

    {

    try

    {

    PropertyInfo xinfo = null, yinfo = null;

    object subitemx = null, subitemy = null;

    if (subprops != null)

    {


    foreach (string item in subprops)

    {

    if ((subitemx != null) && (subitemy != null))

    {

    subitemx = xinfo.GetType() .GetProperty(it em).GetValue(x, null);

    subitemy = yinfo.GetType() .GetProperty(it em).GetValue(y, null);

    if ((subitemx == null) && (subitemy == null))

    {

    //Item not found

    break;

    }

    }

    else

    {

    subitemx = x.GetType().Get Property(item). GetValue(x, null);

    subitemy = y.GetType().Get Property(item). GetValue(y, null);

    }


    }

    }

    object xvalue = null, yvalue = null;

    if ((subitemy != null) && (subitemx != null))

    {

    xvalue =
    subitemx.GetTyp e().GetProperty (this.compareby ).GetValue(subi temx,null);

    yvalue =
    subitemy.GetTyp e().GetProperty (this.compareby ).GetValue(subi temy,null);

    if ((yvalue != null) && (xvalue != null))

    {

    return Comparer.Defaul tInvariant.Comp are(xvalue, yvalue);


    }

    }

    else

    {

    xvalue = x.GetType().Get Property(this.c ompareby).GetVa lue(subitemx, null);

    yvalue = y.GetType().Get Property(this.c ompareby).GetVa lue(subitemy, null);

    if ((yvalue != null) && (xvalue != null))

    {

    return Comparer.Defaul tInvariant.Comp are(xvalue, yvalue);

    }

    }

    }

    catch (Exception)

    {

    }

    return 0;

    }

    #endregion

    }

    }























  • sloan

    #2
    Re: Sorting ArrayLists by Multipul Columns

    See


    6/19/2006
    Advanced IComparer // Sorting on Multiple Values



    "iKiLL" <iKill@NotMyEma il.comwrote in message
    news:%23yBbw$AC HHA.1224@TK2MSF TNGP04.phx.gbl. ..
    Hi All,
    >
    My background is that i am new to C- Sharp and OOP. I have been working
    with
    it for about 2 Weeks. I do have good experionce in developing in things
    like
    VB6, COM, ASP, JavaScript and i have done some development with VB.Net but
    it was not really done in an OOP fashion. i have Been asked to take over a
    PDA application that has been writen in C-Sharp 2005.
    >
    My Question.
    The application is holting in Memory an ArrayList of Tasks that it has
    De-Serialized from an XML data file on the PDA.
    >
    Now I need to Sort the data buy 3 Columns (These have been setup as
    properties of the object "I Think?") just like you would in an SQL
    (OrderBy
    Age, Fname, Lname)
    >
    I have been reading all day and i still can not figure out how to do this.
    Below is a Code Snippet i think might work but i reall am batteling to
    understand it and i Can not figger out how to use it to get what i want.
    >
    If some one could please help me by just comment each line for me with and
    explenation on what each part of it means and how it is working i would be
    very greatefull.
    Even the smallest details would be most beneficial for me, and would be
    much
    appriciated.
    >
    Many Thanks,
    Ink
    >
    >
    >
    >
    using System;
    >
    using System.Collecti ons.Generic;
    >
    using System.Text;
    >
    using System.Collecti ons;
    >
    using System.Reflecti on;
    >
    namespace PPCAssetClient. Helpers
    >
    {
    >
    public class ArrayCompareCla ss : IComparer
    >
    {
    >
    private string compareby = "";
    >
    private string[] subprops = null;
    >
    #region IComparer Members
    >
    public void SortBy(string compareby)
    >
    {
    >
    this.compareby = compareby;
    >
    }
    >
    public string[] SubProperties
    >
    {
    >
    get { return subprops; }
    >
    set { subprops=value; }
    >
    }
    >
    public int Compare(object x, object y)
    >
    {
    >
    try
    >
    {
    >
    PropertyInfo xinfo = null, yinfo = null;
    >
    object subitemx = null, subitemy = null;
    >
    if (subprops != null)
    >
    {
    >
    >
    foreach (string item in subprops)
    >
    {
    >
    if ((subitemx != null) && (subitemy != null))
    >
    {
    >
    subitemx = xinfo.GetType() .GetProperty(it em).GetValue(x, null);
    >
    subitemy = yinfo.GetType() .GetProperty(it em).GetValue(y, null);
    >
    if ((subitemx == null) && (subitemy == null))
    >
    {
    >
    //Item not found
    >
    break;
    >
    }
    >
    }
    >
    else
    >
    {
    >
    subitemx = x.GetType().Get Property(item). GetValue(x, null);
    >
    subitemy = y.GetType().Get Property(item). GetValue(y, null);
    >
    }
    >
    >
    }
    >
    }
    >
    object xvalue = null, yvalue = null;
    >
    if ((subitemy != null) && (subitemx != null))
    >
    {
    >
    xvalue =
    subitemx.GetTyp e().GetProperty (this.compareby ).GetValue(subi temx,null);
    >
    yvalue =
    subitemy.GetTyp e().GetProperty (this.compareby ).GetValue(subi temy,null);
    >
    if ((yvalue != null) && (xvalue != null))
    >
    {
    >
    return Comparer.Defaul tInvariant.Comp are(xvalue, yvalue);
    >
    >
    }
    >
    }
    >
    else
    >
    {
    >
    xvalue = x.GetType().Get Property(this.c ompareby).GetVa lue(subitemx, null);
    >
    yvalue = y.GetType().Get Property(this.c ompareby).GetVa lue(subitemy, null);
    >
    if ((yvalue != null) && (xvalue != null))
    >
    {
    >
    return Comparer.Defaul tInvariant.Comp are(xvalue, yvalue);
    >
    }
    >
    }
    >
    }
    >
    catch (Exception)
    >
    {
    >
    }
    >
    return 0;
    >
    }
    >
    #endregion
    >
    }
    >
    }
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >

    Comment

    • iKiLL

      #3
      Re: Sorting ArrayLists by Multipul Columns

      Thanks sloan
      i'll give it a look.




      "sloan" <sloan@ipass.ne twrote in message
      news:eMfM5KCCHH A.1012@TK2MSFTN GP04.phx.gbl...
      See

      >
      6/19/2006
      Advanced IComparer // Sorting on Multiple Values
      >
      >
      >
      "iKiLL" <iKill@NotMyEma il.comwrote in message
      news:%23yBbw$AC HHA.1224@TK2MSF TNGP04.phx.gbl. ..
      >Hi All,
      >>
      >My background is that i am new to C- Sharp and OOP. I have been working
      with
      >it for about 2 Weeks. I do have good experionce in developing in things
      like
      >VB6, COM, ASP, JavaScript and i have done some development with VB.Net
      >but
      >it was not really done in an OOP fashion. i have Been asked to take over
      >a
      >PDA application that has been writen in C-Sharp 2005.
      >>
      >My Question.
      >The application is holting in Memory an ArrayList of Tasks that it has
      >De-Serialized from an XML data file on the PDA.
      >>
      >Now I need to Sort the data buy 3 Columns (These have been setup as
      >properties of the object "I Think?") just like you would in an SQL
      (OrderBy
      >Age, Fname, Lname)
      >>
      >I have been reading all day and i still can not figure out how to do
      >this.
      >Below is a Code Snippet i think might work but i reall am batteling to
      >understand it and i Can not figger out how to use it to get what i want.
      >>
      >If some one could please help me by just comment each line for me with
      >and
      >explenation on what each part of it means and how it is working i would
      >be
      >very greatefull.
      >Even the smallest details would be most beneficial for me, and would be
      much
      >appriciated.
      >>
      >Many Thanks,
      >Ink
      >>
      >>
      >>
      >>
      >using System;
      >>
      >using System.Collecti ons.Generic;
      >>
      >using System.Text;
      >>
      >using System.Collecti ons;
      >>
      >using System.Reflecti on;
      >>
      >namespace PPCAssetClient. Helpers
      >>
      >{
      >>
      >public class ArrayCompareCla ss : IComparer
      >>
      >{
      >>
      >private string compareby = "";
      >>
      >private string[] subprops = null;
      >>
      >#region IComparer Members
      >>
      >public void SortBy(string compareby)
      >>
      >{
      >>
      >this.compare by = compareby;
      >>
      >}
      >>
      >public string[] SubProperties
      >>
      >{
      >>
      >get { return subprops; }
      >>
      >set { subprops=value; }
      >>
      >}
      >>
      >public int Compare(object x, object y)
      >>
      >{
      >>
      >try
      >>
      >{
      >>
      >PropertyInfo xinfo = null, yinfo = null;
      >>
      >object subitemx = null, subitemy = null;
      >>
      >if (subprops != null)
      >>
      >{
      >>
      >>
      >foreach (string item in subprops)
      >>
      >{
      >>
      >if ((subitemx != null) && (subitemy != null))
      >>
      >{
      >>
      >subitemx = xinfo.GetType() .GetProperty(it em).GetValue(x, null);
      >>
      >subitemy = yinfo.GetType() .GetProperty(it em).GetValue(y, null);
      >>
      >if ((subitemx == null) && (subitemy == null))
      >>
      >{
      >>
      >//Item not found
      >>
      >break;
      >>
      >}
      >>
      >}
      >>
      >else
      >>
      >{
      >>
      >subitemx = x.GetType().Get Property(item). GetValue(x, null);
      >>
      >subitemy = y.GetType().Get Property(item). GetValue(y, null);
      >>
      >}
      >>
      >>
      >}
      >>
      >}
      >>
      >object xvalue = null, yvalue = null;
      >>
      >if ((subitemy != null) && (subitemx != null))
      >>
      >{
      >>
      >xvalue =
      >subitemx.GetTy pe().GetPropert y(this.compareb y).GetValue(sub itemx,null);
      >>
      >yvalue =
      >subitemy.GetTy pe().GetPropert y(this.compareb y).GetValue(sub itemy,null);
      >>
      >if ((yvalue != null) && (xvalue != null))
      >>
      >{
      >>
      >return Comparer.Defaul tInvariant.Comp are(xvalue, yvalue);
      >>
      >>
      >}
      >>
      >}
      >>
      >else
      >>
      >{
      >>
      >xvalue = x.GetType().Get Property(this.c ompareby).GetVa lue(subitemx,
      >null);
      >>
      >yvalue = y.GetType().Get Property(this.c ompareby).GetVa lue(subitemy,
      >null);
      >>
      >if ((yvalue != null) && (xvalue != null))
      >>
      >{
      >>
      >return Comparer.Defaul tInvariant.Comp are(xvalue, yvalue);
      >>
      >}
      >>
      >}
      >>
      >}
      >>
      >catch (Exception)
      >>
      >{
      >>
      >}
      >>
      >return 0;
      >>
      >}
      >>
      >#endregion
      >>
      >}
      >>
      >}
      >>
      >>
      >>
      >>
      >>
      >>
      >>
      >>
      >>
      >>
      >>
      >>
      >>
      >>
      >>
      >>
      >>
      >>
      >>
      >>
      >>
      >>
      >>
      >
      >

      Comment

      Working...