Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#对数据表进行自定义排序_C# - Fatal编程技术网

c#对数据表进行自定义排序

c#对数据表进行自定义排序,c#,C#,我有一个datatable中的数据,需要按以下方式在第一列上排序: A02 BLANK0010 D02 BLANK0007 B04 BLANK0011 G05 BLANK0012 C06 BLANK0014 E08 BLANK0013 F10 BLANK0016 H12 BLANK0015 B02 G112486 C02 G125259 E02 G125257 F02 G112492 G02 G125095 H02 G112489 A03 G1

我有一个datatable中的数据,需要按以下方式在第一列上排序:

A02 BLANK0010 
D02 BLANK0007 
B04 BLANK0011 
G05 BLANK0012 
C06 BLANK0014 
E08 BLANK0013 
F10 BLANK0016 
H12 BLANK0015 
B02 G112486   
C02 G125259   
E02 G125257   
F02 G112492   
G02 G125095   
H02 G112489   
A03 G125090   
B03 G112499   
C03 G125256   
D03 G002007   
E03 G112494   
F03 G002005   
G03 G112495   
H03 G002008   
A04 G115717   
如果我做常规排序,它将像这样排序:A02,A03,A04。但我需要A02,B02,C02。。。等

我怎么做>?以下是我迄今为止的代码:

DataView=dt.DefaultView;
view.Sort=“position”

您需要进行自定义排序。有关提示,请参见以下问题:


您可能需要将第一列拆分为两个单独的列。

您需要进行自定义排序。有关提示,请参见以下问题:


您可能希望将第一列拆分为两个单独的列。

尽可能的方法:添加将第一列的第一个字母放在一起的列,然后按该列和第一列排序。

尽可能的方法:添加将第一列的第一个字母放在一起的列,然后按该列和第一列排序。

可能需要重构,但解决了问题

//group by the rows by splitting values of column
var groupBy = table.AsEnumerable()
                .GroupBy(o => 
                    Regex.Replace(o["position"].ToString(), @"[0-9]", ""));

 var dataRows = Sort(groupBy);
下面是排序方法:

//yield the first row of each group
private static IEnumerable<DataRow> Sort(IEnumerable<IGrouping<string, DataRow>> groupByCollection)
{
    //sort each character group(e.g. A,B) by integer part of their values
    var groupings =
        groupByCollection.Select(
            o =>
            new
                {
                    o.Key,
                    Value = o.OrderBy(a => Regex.Replace(a["position"].ToString(), "[a-z]", "", RegexOptions.IgnoreCase)).ToArray()
                }).ToArray();

    int i = 0, j;
    for (j = 0; j < groupings[i].Value.Length; j++,i=0)
        for (i = 0; i < groupings.Length; i++)
        {
            yield return groupings[i].Value[j];
        }
}
//生成每个组的第一行
私有静态IEnumerable排序(IEnumerable groupByCollection)
{
//按其值的整数部分对每个字符组(例如A、B)进行排序
var分组=
groupByCollection。选择(
o=>
新的
{
o、 钥匙,
Value=o.OrderBy(a=>Regex.Replace(a[“position”].ToString(),“[a-z],”,RegexOptions.IgnoreCase)).ToArray()
}).ToArray();
int i=0,j;
对于(j=0;j
可能需要重构,但解决了问题

//group by the rows by splitting values of column
var groupBy = table.AsEnumerable()
                .GroupBy(o => 
                    Regex.Replace(o["position"].ToString(), @"[0-9]", ""));

 var dataRows = Sort(groupBy);
下面是排序方法:

//yield the first row of each group
private static IEnumerable<DataRow> Sort(IEnumerable<IGrouping<string, DataRow>> groupByCollection)
{
    //sort each character group(e.g. A,B) by integer part of their values
    var groupings =
        groupByCollection.Select(
            o =>
            new
                {
                    o.Key,
                    Value = o.OrderBy(a => Regex.Replace(a["position"].ToString(), "[a-z]", "", RegexOptions.IgnoreCase)).ToArray()
                }).ToArray();

    int i = 0, j;
    for (j = 0; j < groupings[i].Value.Length; j++,i=0)
        for (i = 0; i < groupings.Length; i++)
        {
            yield return groupings[i].Value[j];
        }
}
//生成每个组的第一行
私有静态IEnumerable排序(IEnumerable groupByCollection)
{
//按其值的整数部分对每个字符组(例如A、B)进行排序
var分组=
groupByCollection。选择(
o=>
新的
{
o、 钥匙,
Value=o.OrderBy(a=>Regex.Replace(a[“position”].ToString(),“[a-z],”,RegexOptions.IgnoreCase)).ToArray()
}).ToArray();
int i=0,j;
对于(j=0;j
有点原始,但有效的方法(在本例中):

dv.Sort=子字符串(字段,2,3)+子字符串(字段,1,1)


一种简单但有效的方法(在本例中):

dv.Sort=子字符串(字段,2,3)+子字符串(字段,1,1)


第一列总是一个字符后跟一个数字吗?检查我的解决方案伙伴,它将解决您的问题,如果有任何问题,请留下评论。第一列总是一个字符后跟一个数字吗?检查我的解决方案伙伴,它将解决您的问题,如果有任何问题,请留下注释。@herrow-有关计算列表达式的
子字符串
函数,请参阅。但这都是附加条件,即字符到数字的模式是一致的。@herrow-请参阅:有关计算列表达式的
子字符串
函数。但这都是附加条件,即字符到数字的模式是一致的。