1

我想对 object.Frequency 的组合框中的项目进行排序。我做了一些研究,然后我上了这门课:

 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());
    }
}

然后,将我的对象放入组合框(未排序)中,我使用:

private void showGenderfreq()
{
    cboGenderFreqs.Items.Clear();
    foreach (GenderFrequency gf in GenderFrequency.GenderFrequencies(
             Bird.getBirdFromCSV(txtFile.Text)))
    {
        cboGenderFreqs.Items.Add(gf);
    }
}

但显然我希望将该组合框排序为频率。现在在哪里:

  1. Accipiter(2)
  2. 阿劳达 (1)
  3. 阿纳斯 (6) ...

它应该是

  1. 阿纳斯(6)
  2. Accipiter(2)
  3. 阿劳达(1)

先感谢您

4

1 回答 1

1

好吧,这样做不是更容易吗

cboGenderFreqs.Items.Clear();
cboGenderFreqs.Items.AddRange(
   GenderFrequency.GenderFrequencies.OrderByDescending(gf => gf.Frequency)
   .ToArray());
于 2013-09-03T09:21:37.780 回答