I have a very messy long items full of strings in a combobox, and it would be lovely to just sort it from a to z to make it easier to track. Is it possible?
-
2Are you using an object (like an array or `List
`) as a `.DataSource` for the combobox, or are you populating the `.Items` by yourself manually? – Dai Jun 13 '13 at 06:48 -
check this one. http://stackoverflow.com/questions/188141/c-sharp-list-orderby-alphabetical-order – Narendra Jun 13 '13 at 06:49
-
yes the source of the ComboBox itself is an array of strings – Hendra Anggrian Jun 13 '13 at 06:50
-
Depends on what UI technology you're working with. WinForms ComboBoxes have a [`Sorted`](http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.sorted.aspx) property. But you haven't told us what you're building. WinForms? WPF? HTML? – Damien_The_Unbeliever Jun 13 '13 at 06:50
-
Have you tried: ComponentModel:SortDescription? Its in the WindowBase.dll – Master117 Jun 13 '13 at 06:51
-
if you are sorting in code-behind, you can use linq before databinding: myList.OrderBy(x=>x.MyKey); – Jun 13 '13 at 06:54
-
my bad, I'm building Windows 8 Store apps with C# and XAML – Hendra Anggrian Jun 13 '13 at 07:07
4 Answers
There are two possible ways that I could think of:
A) Use the WinForms Combobox Sorted
Property
If you're using WinForms, you can use ComboBox.Sorted = true;
B) Manually Sort your List with OrderBy
If the data in your combo box comes from in a form of a list, use OrderBy to the List
of data you are going to put in the ComboBox
before setting it.
Here's an example:
var myList = new List<string>() {"q","w","e","r","t","y"};
var sorted = a.OrderBy(c => c).ToArray()
comboBox1.Items.AddRange(sorted);
turns out I can answer my own question, in Windows 8 C# app, ComboBox has Sort() properties that would simply arrange every items from a to z. Thanks.

- 5,780
- 13
- 57
- 97
first I did not populate my dropdown combobox directly like this
AppCombBox1.Items.Add(comboboxvalue);
but instead i populated a list with same data (done in loop in code, which is searching through ini file for values):
List<string> TempList = new List<string>();
TempList.Add(comboboxvalue);
after list is populated, I sort it and after that I fill dropdown combobox with sorted values.
TempList.Sort();
foreach (string ListValue in TempList)
{
AppCombBox1.Items.Add(ListValue);
}

- 1
Just set the Sorted Property to True
. It will sort the elements. I am fetching values from database and it works fine.

- 2,851
- 5
- 22
- 39