Click here to Skip to main content
15,921,884 members
Home / Discussions / C#
   

C#

 
QuestionHow do you sort a listBox by number value rather than alphabetically Pin
Nathan Revka7-Aug-09 22:17
Nathan Revka7-Aug-09 22:17 
AnswerRe: How do you sort a listBox by number value rather than alphabetically Pin
Henry Minute8-Aug-09 0:01
Henry Minute8-Aug-09 0:01 
AnswerRe: How do you sort a listBox by number value rather than alphabetically Pin
Moreno Airoldi8-Aug-09 0:03
Moreno Airoldi8-Aug-09 0:03 
AnswerRe: How do you sort a listBox by number value rather than alphabetically [modified] Pin
Luc Pattyn8-Aug-09 0:13
sitebuilderLuc Pattyn8-Aug-09 0:13 
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]


Smile | :)

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

QuestionHow do you sort a listBox by number value rather than alphabetically Pin
Nathan Revka7-Aug-09 22:17
Nathan Revka7-Aug-09 22:17 
QuestionSplit text and Store into Arry List Pin
M Riaz Bashir7-Aug-09 20:28
M Riaz Bashir7-Aug-09 20:28 
AnswerRe: Split text and Store into Arry List Pin
Abhijit Jana7-Aug-09 21:42
professionalAbhijit Jana7-Aug-09 21:42 
GeneralRe: Split text and Store into Arry List Pin
OriginalGriff7-Aug-09 22:04
mveOriginalGriff7-Aug-09 22:04 
GeneralRe: Split text and Store into Arry List Pin
Abhijit Jana7-Aug-09 22:17
professionalAbhijit Jana7-Aug-09 22:17 
AnswerRe: Split text and Store into Arry List Pin
Arindam Sinha7-Aug-09 21:58
Arindam Sinha7-Aug-09 21:58 
AnswerRe: Split text and Store into Arry List Pin
Mycroft Holmes7-Aug-09 22:07
professionalMycroft Holmes7-Aug-09 22:07 
QuestionCrystal Report Error... Pin
Rahul DSG7-Aug-09 20:19
Rahul DSG7-Aug-09 20:19 
QuestionC# Save Application Layout on application exit and reload it on application start. [modified] Pin
shaktisinh7-Aug-09 18:01
shaktisinh7-Aug-09 18:01 
AnswerRe: C# Save Application Layout on application exit and reload it on application start. Pin
N a v a n e e t h7-Aug-09 18:15
N a v a n e e t h7-Aug-09 18:15 
AnswerRe: C# Save Application Layout on application exit and reload it on application start. Pin
stancrm7-Aug-09 19:17
stancrm7-Aug-09 19:17 
AnswerRe: C# Save Application Layout on application exit and reload it on application start. Pin
Arindam Sinha7-Aug-09 22:03
Arindam Sinha7-Aug-09 22:03 
GeneralRe: C# Save Application Layout on application exit and reload it on application start. Pin
shaktisinh12-Aug-09 3:27
shaktisinh12-Aug-09 3:27 
GeneralMessage Removed Pin
13-Aug-09 0:35
shaktisinh13-Aug-09 0:35 
GeneralRe: C# Save Application Layout on application exit and reload it on application start. Pin
Arindam Sinha15-Aug-09 13:14
Arindam Sinha15-Aug-09 13:14 
GeneralRe: C# Save Application Layout on application exit and reload it on application start. Pin
shaktisinh16-Aug-09 19:40
shaktisinh16-Aug-09 19:40 
GeneralRe: C# Save Application Layout on application exit and reload it on application start. Pin
Arindam Sinha16-Aug-09 22:29
Arindam Sinha16-Aug-09 22:29 
AnswerRe: C# Save Application Layout on application exit and reload it on application start. Pin
Mycroft Holmes7-Aug-09 22:09
professionalMycroft Holmes7-Aug-09 22:09 
QuestionListview Subitems index out of range Pin
iNutR7-Aug-09 16:48
iNutR7-Aug-09 16:48 
AnswerRe: Listview Subitems index out of range Pin
Arindam Sinha7-Aug-09 22:12
Arindam Sinha7-Aug-09 22:12 
AnswerRe: Listview Subitems index out of range Pin
Mycroft Holmes7-Aug-09 22:16
professionalMycroft Holmes7-Aug-09 22:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.