Sorting ArrayLists by Multipul Columns

I

iKiLL

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.Collections.Generic;

using System.Text;

using System.Collections;

using System.Reflection;

namespace PPCAssetClient.Helpers

{

public class ArrayCompareClass : 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(item).GetValue(x, null);

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

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

{

//Item not found

break;

}

}

else

{

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

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

}


}

}

object xvalue = null, yvalue = null;

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

{

xvalue =
subitemx.GetType().GetProperty(this.compareby).GetValue(subitemx,null);

yvalue =
subitemy.GetType().GetProperty(this.compareby).GetValue(subitemy,null);

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

{

return Comparer.DefaultInvariant.Compare(xvalue, yvalue);


}

}

else

{

xvalue = x.GetType().GetProperty(this.compareby).GetValue(subitemx, null);

yvalue = y.GetType().GetProperty(this.compareby).GetValue(subitemy, null);

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

{

return Comparer.DefaultInvariant.Compare(xvalue, yvalue);

}

}

}

catch (Exception)

{

}

return 0;

}

#endregion

}

}
 
S

sloan

See
http://sholliday.spaces.live.com/blog/

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



iKiLL said:
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.Collections.Generic;

using System.Text;

using System.Collections;

using System.Reflection;

namespace PPCAssetClient.Helpers

{

public class ArrayCompareClass : 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(item).GetValue(x, null);

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

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

{

//Item not found

break;

}

}

else

{

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

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

}


}

}

object xvalue = null, yvalue = null;

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

{

xvalue =
subitemx.GetType().GetProperty(this.compareby).GetValue(subitemx,null);

yvalue =
subitemy.GetType().GetProperty(this.compareby).GetValue(subitemy,null);

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

{

return Comparer.DefaultInvariant.Compare(xvalue, yvalue);


}

}

else

{

xvalue = x.GetType().GetProperty(this.compareby).GetValue(subitemx, null);

yvalue = y.GetType().GetProperty(this.compareby).GetValue(subitemy, null);

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

{

return Comparer.DefaultInvariant.Compare(xvalue, yvalue);

}

}

}

catch (Exception)

{

}

return 0;

}

#endregion

}

}
 
I

iKiLL

Thanks sloan
i'll give it a look.




sloan said:
See
http://sholliday.spaces.live.com/blog/

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



iKiLL said:
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.Collections.Generic;

using System.Text;

using System.Collections;

using System.Reflection;

namespace PPCAssetClient.Helpers

{

public class ArrayCompareClass : 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(item).GetValue(x, null);

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

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

{

//Item not found

break;

}

}

else

{

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

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

}


}

}

object xvalue = null, yvalue = null;

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

{

xvalue =
subitemx.GetType().GetProperty(this.compareby).GetValue(subitemx,null);

yvalue =
subitemy.GetType().GetProperty(this.compareby).GetValue(subitemy,null);

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

{

return Comparer.DefaultInvariant.Compare(xvalue, yvalue);


}

}

else

{

xvalue = x.GetType().GetProperty(this.compareby).GetValue(subitemx,
null);

yvalue = y.GetType().GetProperty(this.compareby).GetValue(subitemy,
null);

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

{

return Comparer.DefaultInvariant.Compare(xvalue, yvalue);

}

}

}

catch (Exception)

{

}

return 0;

}

#endregion

}

}
 

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

Top