Reputation: 67
I want to sort the items in a combobox on the object.Frequency. I did some research and then I made this class:
public class CompareByFrequency : IComparer<GenderFrequency>
{
public int Compare(GenderFrequency x, GenderFrequency y)
{
return x.Frequency.CompareTo(y.Frequency);
}
public static void QSFreq(List<GenderFrequency> g)
{
g.Sort(new CompareByFrequency());
}
}
Then, to put my objects in the combobox (unsorted) I use:
private void showGenderfreq()
{
cboGenderFreqs.Items.Clear();
foreach (GenderFrequency gf in GenderFrequency.GenderFrequencies(
Bird.getBirdFromCSV(txtFile.Text)))
{
cboGenderFreqs.Items.Add(gf);
}
}
But obviously I want that combobox to be sorted to Frequency. Where it is now:
it Should be
Thank you in advance
Upvotes: 1
Views: 348
Reputation: 35746
Well, wouldn't it be easier to do
cboGenderFreqs.Items.Clear();
cboGenderFreqs.Items.AddRange(
GenderFrequency.GenderFrequencies.OrderByDescending(gf => gf.Frequency)
.ToArray());
Upvotes: 1