自然排序Datgridview [英] Natural Sort for Datgridview

查看:116
本文介绍了自然排序Datgridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个datagrid的列,允许用户输入可能包含数字的字符串。我已经以编程方式进行了排序,另一个是自动排序的。
我查看了这个和其他网站上的所有可用信息,但没有发现任何有用的信息,或在某些情况下,我能够知道如何实现。
许多解决方案使用Icomparer,但很少是专门用于datagridview的。
这是执行datagridview1.Sort(Column1,ListSortDirection.Ascending)后的列数据的示例;

  1 
140
2b
40
70
7a
9
aa
aa30
aa5
bc
de

我已经使用本网站的解决方案介绍了MyDatagridHelper类和相关代码:
C#:自定义类型的DataGridView
但可以给出以下结果

  aa 
bc
de
aa30
2b
7a
70
1
40
140
9
aa5

所有其他解决方案不是特定于datagridview的。我自学,编码相对较新,所以我无法理解代码。我尽可能地尝试找到共同点,剪切和粘贴,但无济于事。
一些我认为显示出希望的例子,可能做的是:
https://www.codeproject.com/articles/22517/natural-sort-comparer

C#中的自然排序顺序



我有效地需要帮助如何实现这些或任何其他解决方案可能会做这个工作。
我没有包括第二列的简单性,因为它与问题无关。
感谢

解决方案

您链接到的代码项目文章22517具有您需要的逻辑,只是需要通过创建 System.Collections.IComparer 而不是<$ c $的实现来实现 DataGridView 的一些调整。 c> System.Collections.Generic.Comparer



所以如果你在你的项目中创建一个新的类,就像这样:

  public class NaturalSortComparer:System.Collections.IComparer {

private System.Collections.Generic.Dictionary< string,string []>表;

public NaturalSortComparer(){
table = new System.Collections.Generic.Dictionary< string,string []>();
}

public void Dispose(){
table.Clear();
table = null;
}

public int Compare(object x,object y){
System.Windows.Forms.DataGridViewRow DataGridViewRow1 =(System.Windows.Forms.DataGridViewRow)x;
System.Windows.Forms.DataGridViewRow DataGridViewRow2 =(System.Windows.Forms.DataGridViewRow)y;

string xStr = DataGridViewRow1.Cells [Column1]。Value.ToString();
string yStr = DataGridViewRow2.Cells [Column1]。Value.ToString();


if(xStr == yStr){
return 0;
}
string [] x1,y1;
if(!table.TryGetValue(xStr,out x1)){
x1 = System.Text.RegularExpressions.Regex.Split(xStr.Replace(,),([0- 9] +));
table.Add(xStr,x1);
}
if(!table.TryGetValue(yStr,out y1)){
y1 = System.Text.RegularExpressions.Regex.Split(yStr.Replace(,), ([0-9] +));
table.Add(yStr,y1); (int i = 0; i< x1.Length&& i< y1.Length; i ++){
if(x1 [i] != y1 [i]){
返回PartCompare(x1 [i],y1 [i]);
}
}
if(y1.Length> x1.Length){
return 1;
} else if(x1.Length> y1.Length){
return -1;
} else {
return 0;
}
}

private static int PartCompare(string left,string right){
int x,y;
if(!int.TryParse(left,out x)){
return left.CompareTo(right);
}

if(!int.TryParse(right,out y)){
return left.CompareTo(right);
}

返回x.CompareTo(y);
}
}

你可以在这里看到我已经硬编码了根据您的示例使用名为Column1的列,但您可以将其更改为更具动态性。



当您对网格进行排序时,可以传入刚刚创建的这个类的新实例,如下所示:

  dataGridView1.Sort(new NaturalSortComparer()); 


I have a datagrid with a column that allows entry by the user of strings which may contain numbers. The column I have made sorted programmatically and one other automatically. I have looked at all available information on this and other websites but have found nothing that works or in some cases that I am able to know how to implement. Many of the solutions use Icomparer but few are specifically for datagridview. This is an example of the column data after performing datagridview1.Sort(Column1, ListSortDirection.Ascending);

1
140
2b
40
70
7a
9
aa
aa30
aa5
bc
de

I have introduced the MyDatagridHelper Class and relevant code using the solution from this site: C#: Custom sort of DataGridView, but alas it gives the following results

aa
bc
de
aa30
2b
7a
70
1
40
140
9
aa5

All other solutions are not specific to datagridview. I am self taught and relatively new to coding, so I am not able to make sense of the code within. I have tried as best I can to find commonality and cut and paste but to no avail. Some examples I thought showed promise and might do the job were: https://www.codeproject.com/articles/22517/natural-sort-comparer and Natural Sort Order in C#

I effectively need help on how to implement these or any other solutions that might do the job. I have not included the second column for simplicity as it is not relevant to the question. Thanks

解决方案

The code project article 22517 that you link to has the logic in there that you require, it just needs some tweaking for use with a DataGridView by creating an implementation of System.Collections.IComparer rather than System.Collections.Generic.Comparer

So if you create yourself a new class in your project something like this:

public class NaturalSortComparer : System.Collections.IComparer {

    private System.Collections.Generic.Dictionary<string, string[]> table;

    public NaturalSortComparer() {
        table = new System.Collections.Generic.Dictionary<string, string[]>();
    }

    public void Dispose() {
        table.Clear();
        table = null;
    }

    public int Compare(object x, object y) {
        System.Windows.Forms.DataGridViewRow DataGridViewRow1 = (System.Windows.Forms.DataGridViewRow)x;
        System.Windows.Forms.DataGridViewRow DataGridViewRow2 = (System.Windows.Forms.DataGridViewRow)y;

        string xStr = DataGridViewRow1.Cells["Column1"].Value.ToString();
        string yStr = DataGridViewRow2.Cells["Column1"].Value.ToString();


        if (xStr == yStr) {
            return 0;
        }
        string[] x1, y1;
        if (!table.TryGetValue(xStr, out x1)) {
            x1 = System.Text.RegularExpressions.Regex.Split(xStr.Replace(" ", ""), "([0-9]+)");
            table.Add(xStr, x1);
        }
        if (!table.TryGetValue(yStr, out y1)) {
            y1 = System.Text.RegularExpressions.Regex.Split(yStr.Replace(" ", ""), "([0-9]+)");
            table.Add(yStr, y1);
        }

        for (int i = 0; i < x1.Length && i < y1.Length; i++) {
            if (x1[i] != y1[i]) {
                return PartCompare(x1[i], y1[i]);
            }
        }
        if (y1.Length > x1.Length) {
            return 1;
        } else if (x1.Length > y1.Length) {
            return -1;
        } else {
            return 0;
        }
    }

    private static int PartCompare(string left, string right) {
        int x, y;
        if (!int.TryParse(left, out x)) {
            return left.CompareTo(right);
        }

        if (!int.TryParse(right, out y)) {
            return left.CompareTo(right);
        }

        return x.CompareTo(y);
    }
}

You can see in here that I have hard coded it to used a column named "Column1" as per your example, but you can change this to be more dynamic.

When you then sort your grid, you can pass in a new instance of this class you have just created, like this:

dataGridView1.Sort(new NaturalSortComparer());

这篇关于自然排序Datgridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆