How do you sort a listBox by number value rather than alphabetically
-
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?
-
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?
Here is one way MSDN - ListBox.Sort Method[^]. Alternatively you can put your
string
andint
into a class and implement theIComparable
interface. Then, possibly, put them into a collection to add via theListBox.DataSource
propertyHenry 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.”
-
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?
You can implement a custom type for
Card
s and override theCompareTo()
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;
-
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?
Hi, if your ListBox (or any other collection) contains items of type
T
, then you could provide an object implementingIComparer<T>
and pass that to theSort
method. All that interface takes is providing a simple methodint 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