Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.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# 如何自定义DataTable列的排序_C#_Asp.net_Sorting_Datatable - Fatal编程技术网

C# 如何自定义DataTable列的排序

C# 如何自定义DataTable列的排序,c#,asp.net,sorting,datatable,C#,Asp.net,Sorting,Datatable,我需要对数据表列的值进行排序。该列包含字符串、整数或混合文本。例如: 数据表列包含如下值:23,18,12,store 23,store a1,1283,25,… 如果我使用Dataview.sort()方法对值进行排序,结果如下:12、1283、18、23、25、store 1283、store a1、但我需要这样:12、18、23、25、1283、store 23、store a1、 有什么简单的方法可以达到这个要求吗?你不能直接按照你的自定义标准。您必须编写自己的比较代码 看看这个列的数据

我需要对数据表列的值进行排序。该列包含字符串、整数或混合文本。例如:

数据表列包含如下值:
23,18,12,store 23,store a1,1283,25,…

如果我使用
Dataview.sort()
方法对值进行排序,结果如下:
12、1283、18、23、25、store 1283、store a1、
但我需要这样:
12、18、23、25、1283、store 23、store a1、


有什么简单的方法可以达到这个要求吗?

你不能直接按照你的自定义标准。您必须编写自己的比较代码


看看这个

列的数据类型是什么。您发布的数据类似于字母数字,即varchar

您可以使用这行代码对datatable中的数据进行排序。试试这个

datatable.DefaultView.Sort = "COLUMN_NAME ASC"; 

如果没有,您可以重新表述您的问题,指定列的数据类型,因为列既有字母数字值,也有数字值。

标准数据库级别或数据视图类型排序不支持混合类型比较


您可以将原始
数据表中的行
复制到数组中(例如使用
DataTable.rows.CopyTo()
),然后使用自定义比较器调用
数组.Sort()

我认为您应该使用自然排序并创建自己的IComparer

我找到的最好的算法在这里

只需将其设置为一个泛型类(正如linq使用作为linq order by takes IComparer),如下所示

public class AlphanumComparator<T> : IComparer<T>
    {
        private enum ChunkType { Alphanumeric, Numeric };
        private bool InChunk(char ch, char otherCh)
        {
            ChunkType type = ChunkType.Alphanumeric;

            if (char.IsDigit(otherCh))
            {
                type = ChunkType.Numeric;
            }

            if ((type == ChunkType.Alphanumeric && char.IsDigit(ch))
                || (type == ChunkType.Numeric && !char.IsDigit(ch)))
            {
                return false;
            }

            return true;
        }

        public int Compare(T x, T y)
        {
            String s1 = x as string;
            String s2 = y as string;
            if (s1 == null || s2 == null)
            {
                return 0;
            }

            int thisMarker = 0, thisNumericChunk = 0;
            int thatMarker = 0, thatNumericChunk = 0;

            while ((thisMarker < s1.Length) || (thatMarker < s2.Length))
            {
                if (thisMarker >= s1.Length)
                {
                    return -1;
                }
                else if (thatMarker >= s2.Length)
                {
                    return 1;
                }
                char thisCh = s1[thisMarker];
                char thatCh = s2[thatMarker];

                StringBuilder thisChunk = new StringBuilder();
                StringBuilder thatChunk = new StringBuilder();

                while ((thisMarker < s1.Length) && (thisChunk.Length == 0 || InChunk(thisCh, thisChunk[0])))
                {
                    thisChunk.Append(thisCh);
                    thisMarker++;

                    if (thisMarker < s1.Length)
                    {
                        thisCh = s1[thisMarker];
                    }
                }

                while ((thatMarker < s2.Length) && (thatChunk.Length == 0 || InChunk(thatCh, thatChunk[0])))
                {
                    thatChunk.Append(thatCh);
                    thatMarker++;

                    if (thatMarker < s2.Length)
                    {
                        thatCh = s2[thatMarker];
                    }
                }

                int result = 0;
                // If both chunks contain numeric characters, sort them numerically
                if (char.IsDigit(thisChunk[0]) && char.IsDigit(thatChunk[0]))
                {
                    thisNumericChunk = Convert.ToInt32(thisChunk.ToString());
                    thatNumericChunk = Convert.ToInt32(thatChunk.ToString());

                    if (thisNumericChunk < thatNumericChunk)
                    {
                        result = -1;
                    }

                    if (thisNumericChunk > thatNumericChunk)
                    {
                        result = 1;
                    }
                }
                else
                {
                    result = thisChunk.ToString().CompareTo(thatChunk.ToString());
                }

                if (result != 0)
                {
                    return result;
                }
            }

            return 0;
        }


    }
公共类字母计算器:IComparer
{
私有枚举ChunkType{字母数字,数字};
私有布尔InChunk(char ch,char otherCh)
{
ChunkType类型=ChunkType.字母数字;
如果(字符为数字(其他字符))
{
type=ChunkType.Numeric;
}
if((type==ChunkType.Alphanumeric&&char.IsDigit(ch))
||(type==ChunkType.Numeric&&!char.IsDigit(ch)))
{
返回false;
}
返回true;
}
公共整数比较(TX,TY)
{
字符串s1=x作为字符串;
字符串s2=y作为字符串;
如果(s1==null | | s2==null)
{
返回0;
}
int thisMarker=0,thisNumericChunk=0;
int thatMarker=0,thatNumericChunk=0;
而((这个标记=s1.长度)
{
返回-1;
}
else if(该标记>=s2.长度)
{
返回1;
}
char thisCh=s1[thisMarker];
char-thatCh=s2[thattmarker];
StringBuilder thisChunk=新的StringBuilder();
StringBuilder thatChunk=新StringBuilder();
而((thisMarkerthatNumericChunk)
{
结果=1;
}
}
其他的
{
结果=thisChunk.ToString().CompareTo(thatChunk.ToString());
}
如果(结果!=0)
{
返回结果;
}
}
返回0;
}
}
现在,使用linq来应用它

 DataTable dt = new DataTable();
            dt.TableName = "Sort";
            dt.Columns.Add("Check");
            DataRow dr = dt.NewRow();
            dr["Check"] = "12";
            dt.Rows.Add(dr);

            DataRow dr2 = dt.NewRow();
            dr2["Check"] = "1283";
            dt.Rows.Add(dr2);

            DataRow dr3 = dt.NewRow();
            dr3["Check"] = "store 1283";
            dt.Rows.Add(dr3);

            DataRow dr4 = dt.NewRow();
            dr4["Check"] = "23";
            dt.Rows.Add(dr4);

            DataView dv = new DataView();
            dv.Table = dt;

            AlphanumComparator<string> comparer = new AlphanumComparator<string>();
            //DataTable dtNew = dv.Table;
            DataTable dtNew = dv.Table.AsEnumerable().OrderBy(x => x.Field<string>("Check"), comparer).CopyToDataTable();
            dtNew.TableName = "NaturalSort";

            dv.Table = dtNew;
DataTable dt=newdatatable();
dt.TableName=“排序”;
dt.列。添加(“检查”);
DataRow dr=dt.NewRow();
dr[“检查”]=“12”;
dt.Rows.Add(dr);
DataRow dr2=dt.NewRow();
dr2[“检查”]=“1283”;
dt.行。添加(dr2);
DataRow dr3=dt.NewRow();
dr3[“检查”]=“存储1283”;
dt.行。添加(dr3);
DataRow dr4=dt.NewRow();
dr4[“检查”]=“23”;
dt.行。添加(dr4);
DataView dv=新的DataView();
dv.Table=dt;
AlphanumComparator comparer=新的AlphanumComparator();
//DataTable dtNew=dv.Table;
DataTable dtNew=dv.Table.AsEnumerable().OrderBy(x=>x.Field(“检查”),comparer.CopyToDataTable();
dtNew.TableName=“NaturalSort”;
dv.Table=dtNew;

结果12、23、1283、store 1283

您不能直接将其排序到SQL中吗?我认为这将是最有效的,因为您不必重新获取Dataview中的所有数据。排序()此数据取自Sharepoint列表。我们无法对混合类型的数据进行排序。请尝试在数据库级别进行排序,然后添加到DataTable此版本