By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,420 Members | 1,160 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,420 IT Pros & Developers. It's quick & easy.

need help with Compare() function

P: n/a
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;

}

}

Nov 18 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.