Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   ASP .Net (http://www.velocityreviews.com/forums/f29-asp-net.html)
-   -   need help with Compare() function (http://www.velocityreviews.com/forums/t86321-need-help-with-compare-function.html)

Wee Bubba 09-08-2004 07:03 PM

need help with Compare() function
 
i have completed a Compare() function to help me sort the contents of
a ListBox. I wanted to give the user of the class the ability to sort
by EITHER ListItem.Text OR ListItem.Value. ALSO sometimes the text
items/values will be integers and sometimes plain text.

to get around this i implemented 2 public properties on my class. 1
property allows the user to select whether they want to sort by
ListItem.Text or By ListItem.Value property. The other property
allows them to select whether the content type is String or Integer.
i am then looking at these properties and using if/else statements.
but i know that my logic here is messy and prone to error (i am a
beginner) so can anyone suggest a better way for me to achieve this?
thanks. heres my function:


public int Compare(object x, object y)

{

if (dm.contentType == ContentType.String)

{

string sx = "";

string sy = "";

if (dm.sortMode == SortMode.Text)

{

sx = ((ListItem) x).Text;

sy = ((ListItem) y).Text;

}

else

{

sx = ((ListItem) x).Value;

sy = ((ListItem) y).Value;

}

return sx.CompareTo(sy) * this.direction;

}

else

{

int sx = 0;

int sy = 0;

if (dm.sortMode == SortMode.Text)

{

sx = int.Parse(((ListItem) x).Text);

sy = int.Parse(((ListItem) y).Text);

}

else

{

sx = int.Parse(((ListItem) x).Value);

sy = int.Parse(((ListItem) y).Value);

}

return sx.CompareTo(sy) * this.direction;

}

}



All times are GMT. The time now is 09:34 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.