C# 数组排序列表

C# 数组排序列表,c#,datagridview,c#-2.0,C#,Datagridview,C# 2.0,我有一个列表添加的项目如下所示 1 A 333 666 16.02.2013 03:00 10 A 333 666 16.02.2013 03:00 11 A 333 666 16.02.2013 03:00 2 A 333 666 16.02.2013 03:00 2a A 333 666 16.02.2013 03:00 3a A 333 666 16.02.2013 03:00 3b A 333 666 16.02.2013 03:00 4

我有一个
列表
添加的项目如下所示

1   A   333 666 16.02.2013 03:00
10  A   333 666 16.02.2013 03:00
11  A   333 666 16.02.2013 03:00
2   A   333 666 16.02.2013 03:00
2a  A   333 666 16.02.2013 03:00
3a  A   333 666 16.02.2013 03:00
3b  A   333 666 16.02.2013 03:00
4   A   333 666 16.02.2013 03:00
5   A   333 666 16.02.2013 03:00
list.Add(新字符串[]{“1”、“A”、“333”、“666”、“16.02.2013 03:00”)
list.Add(新字符串[]{“2a”、“A”、“333”、“666”、“16.02.2013 03:00”)
list.Add(新字符串[]{“2”、“A”、“333”、“666”、“16.02.2013 03:00”)
list.Add(新字符串[]{“3a”、“A”、“333”、“666”、“16.02.2013 03:00”)
list.Add(新字符串[]{“3b”、“A”、“333”、“666”、“16.02.2013 03:00”)
list.Add(新字符串[]{“4”、“A”、“333”、“666”、“16.02.2013 03:00”)
list.Add(新字符串[]{“5”、“A”、“333”、“666”、“16.02.2013 03:00”)
list.Add(新字符串[]{“10”、“A”、“333”、“666”、“16.02.2013 03:00”)
list.Add(新字符串[]{“11”、“A”、“333”、“666”、“16.02.2013 03:00”)

解析文件中的数据并将其添加到列表后,我必须在DataGridView中显示所有数据,在将所有数据添加到DataGridView后,我希望用户能够通过单击列标题对其进行排序。

问题是,若用户希望按第一列对行进行排序,它将按如下方式排序

1   A   333 666 16.02.2013 03:00
10  A   333 666 16.02.2013 03:00
11  A   333 666 16.02.2013 03:00
2   A   333 666 16.02.2013 03:00
2a  A   333 666 16.02.2013 03:00
3a  A   333 666 16.02.2013 03:00
3b  A   333 666 16.02.2013 03:00
4   A   333 666 16.02.2013 03:00
5   A   333 666 16.02.2013 03:00
但正确的方法是:

1   A   333 666 16.02.2013 03:00
2   A   333 666 16.02.2013 03:00
2a  A   333 666 16.02.2013 03:00
3a  A   333 666 16.02.2013 03:00
3b  A   333 666 16.02.2013 03:00
4   A   333 666 16.02.2013 03:00
5   A   333 666 16.02.2013 03:00
10  A   333 666 16.02.2013 03:00
11  A   333 666 16.02.2013 03:00
我如何使用自然排序按数组的特定索引对字符串数组列表进行排序?
我不能使用LINQ

您正在寻找一个字符串。有网络;我会选择其中一个,然后复制到你的项目中

现在,由于要在DataGridView中排序,因此需要附加到事件并在那里进行自定义排序。它看起来像这样:

private void dataGridView1_SortCompare(object sender, DataGridViewSortCompareEventArgs e)
{
    // Since you want a natural sort in the first column
    if (e.Column.Index == 0)
    {
        // Create an instance of your natural sort comparer here
        IComparer<string> comparer = new YourNaturalComparer()

        // Perform the sort
        e.SortResult = comparer.Compare(
            e.CellValue1.ToString(), e.CellValue2.ToString());

        // Signal that we handled the sorting for this column
        e.Handled = true;
    }
}
private void dataGridView1\u SortCompare(对象发送方、DataGridViews sortCompareEventArgs e)
{
//因为您希望在第一列中使用自然排序
如果(e.Column.Index==0)
{
//在此处创建自然排序比较器的实例
IComparer comparer=新的YourNaturalComparer()
//执行排序
e、 SortResult=比较器。比较(
e、 CellValue1.ToString(),例如CellValue2.ToString();
//表示我们已处理此列的排序
e、 已处理=正确;
}
}

也许您需要一个定制的IComparer。。。把你的逻辑放在这里。DataGridview或List的自定义IComparer?是。它的清单