need help with Compare() function

Discussion in 'ASP .Net' started by Wee Bubba, Sep 8, 2004.

  1. Wee Bubba

    Wee Bubba Guest

    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;

    }

    }
     
    Wee Bubba, Sep 8, 2004
    #1
    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. TN Bella
    Replies:
    0
    Views:
    483
    TN Bella
    Jul 2, 2004
  2. alan jeeves
    Replies:
    10
    Views:
    6,282
    James D. Veale
    Mar 5, 2004
  3. Frank
    Replies:
    1
    Views:
    748
    Dimitre Novatchev
    Sep 12, 2003
  4. Lieven
    Replies:
    3
    Views:
    764
    Rob Williscroft
    Nov 29, 2004
  5. xkenneth
    Replies:
    7
    Views:
    433
Loading...

Share This Page