您好,我有代码来填充组合框,如下所示:
public ListBox fillComboBox(ListBox cb)
{
cb.Items.Clear();
foreach(string[] s in SO)
{
if (s[1].Split(',')[1].Equals("G5IDD"))
{
cb.Items.Add(s[1].Split(',')[3]);
}
}
cb.Sorted = true;
return cb;
}
结果我得到的值排序如下:
2.1 2.10 2.15 2.2 2.20
但我希望它像这样排序
2.1 2.2 2.10 2.15 2.20
SO 是由字符串数组构建的 ArrayList。
有人可以帮我按照我想要的方式排序吗?
提前致谢
编辑:值可以是这样的 4545_3434.2.1/1 4545_3434.2.1/2 4545_3434.2.2 4545_3434.2.2/1
请您参考如下方法:
这是我的建议。不需要 IComparer。这显然假设输入始终采用 [int].[int] 的格式。
public ListBox fillComboBox(ListBox cb)
{
cb.Items.Clear();
foreach(string[] s in SO.ToArray().OrderBy(s => Int32.Parse(s.ToString().Split('.')[0])).ThenBy(s => Int32.Parse(s.ToString().Split('.')[1])))
{
if (s[1].Split(',')[1].Equals("G5IDD"))
{
cb.Items.Add(s[1].Split(',')[3]);
}
}
return cb;
}