Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How do you sort a listBox by number value rather than alphabetically

How do you sort a listBox by number value rather than alphabetically

Scheduled Pinned Locked Moved C#
questionalgorithms
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Nathan Revka
    wrote on last edited by
    #1

    I am storing card displays (eg J♣) in a list box through a list <KeyValuePair<string, int> and I want to order the cards numerically from greatest value to smallest value in the list box. When I change the sorting property on the properties box to "descending", the list is sorted inverse-alphabetically (ie Q is at top and 10 is at the bottom). I want to sort the list descending numerically, by value, so that A is at the top and 2 is at the bottom. How can I do this?

    H M L 3 Replies Last reply
    0
    • N Nathan Revka

      I am storing card displays (eg J♣) in a list box through a list <KeyValuePair<string, int> and I want to order the cards numerically from greatest value to smallest value in the list box. When I change the sorting property on the properties box to "descending", the list is sorted inverse-alphabetically (ie Q is at top and 10 is at the bottom). I want to sort the list descending numerically, by value, so that A is at the top and 2 is at the bottom. How can I do this?

      H Offline
      H Offline
      Henry Minute
      wrote on last edited by
      #2

      Here is one way MSDN - ListBox.Sort Method[^]. Alternatively you can put your string and int into a class and implement the IComparable interface. Then, possibly, put them into a collection to add via the ListBox.DataSource property

      Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

      1 Reply Last reply
      0
      • N Nathan Revka

        I am storing card displays (eg J♣) in a list box through a list <KeyValuePair<string, int> and I want to order the cards numerically from greatest value to smallest value in the list box. When I change the sorting property on the properties box to "descending", the list is sorted inverse-alphabetically (ie Q is at top and 10 is at the bottom). I want to sort the list descending numerically, by value, so that A is at the top and 2 is at the bottom. How can I do this?

        M Offline
        M Offline
        Moreno Airoldi
        wrote on last edited by
        #3

        You can implement a custom type for Cards and override the CompareTo() method to implement custom sorting. Something like:

            internal enum CardSuits
            {
                Spades = 0,
                Hearts,
                Diamonds,
                Clubs
            }
        
            internal enum CardValues
            {
                Two = 0,
                Three,
                Four,
                Five,
                Six,
                Seven,
                Eight,
                Nine,
                Ten,
                Jack,
                Queen,
                King,
                Ace
            }
        
            internal class Card : IComparable
            {
                internal CardSuits Suit;
                internal CardValues Value;
        
                internal Card(CardSuits Suit, CardValues Value)
                {
                    this.Suit = Suit;
                    this.Value = Value;
                }
        
                public int CompareTo(object obj)
                {
                    int MyValue = (int)Value;
                    Card OtherCard = (Card)obj;
                    int OtherValue = (int)OtherCard.Value;
                    return MyValue.CompareTo(OtherValue);
                }
        
                public override string ToString()
                {
                    StringBuilder sb = new StringBuilder();
                    switch (Value)
                    {
                        case CardValues.Two:
                            sb.Append("Two");
                            break;
                        case CardValues.Three:
                            sb.Append("Three");
                            break;
                        case CardValues.Four:
                            sb.Append("Four");
                            break;
                        case CardValues.Five:
                            sb.Append("Five");
                            break;
                        case CardValues.Six:
                            sb.Append("Six");
                            break;
                        case CardValues.Seven:
                            sb.Append("Seven");
                            break;
                        case CardValues.Eight:
                            sb.Append("Eight");
                            break;
                        case CardValues.Nine:
                            sb.Append("Nine");
                            break;
                        case CardValues.Ten:
                            sb.Append("Ten");
                            break;
                        case CardValues.Jack:
                            sb.Append("Jack");
                            break;
        
        1 Reply Last reply
        0
        • N Nathan Revka

          I am storing card displays (eg J♣) in a list box through a list <KeyValuePair<string, int> and I want to order the cards numerically from greatest value to smallest value in the list box. When I change the sorting property on the properties box to "descending", the list is sorted inverse-alphabetically (ie Q is at top and 10 is at the bottom). I want to sort the list descending numerically, by value, so that A is at the top and 2 is at the bottom. How can I do this?

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          Hi, if your ListBox (or any other collection) contains items of type T, then you could provide an object implementing IComparer<T> and pass that to the Sort method. All that interface takes is providing a simple method int Compare(T x, T y) in which you can implement any sorting order you like. ------------------------------------------------------------------------------ FWIW: you can edit and delete your own messages if you need to. [EDIT] Sorry, the above is insufficient, ListBox.Sort() does not take any parameters. There are basically two ways of doing it: 1. implement your own sort method; e.g. a bubble sort where you compare two items (in any way you see fit) and swap them if necessary. 2. better way: use an intermediate collection, e.g. an array. This means: - ListBox.Items.CopyTo(array,0) copies the items into an array - ListBox.Items.Clear() removes the items - Array.Sort(array, IComparer) sorts the items - ListBox.Items.AddRange(array) adds them again, in the right order. [/EDIT] :)

          Luc Pattyn [Forum Guidelines] [My Articles]


          The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


          modified on Saturday, August 8, 2009 3:46 PM

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups