gpt4 book ai didi

c# - 如何自定义 DataTable 列的排序

转载 作者:行者123 更新时间:2023-11-30 19:30:38 27 4
gpt4 key购买 nike

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

数据表列包含如下值: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, ...

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

最佳答案

我认为你应该使用自然排序并制作你自己的 IComparer

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

http://www.davekoelle.com/files/AlphanumComparator.cs .

只需将其设为通用类(因为 linq 使用 IComparer 作为 Linq order by takes),如下所示

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;
}


}

现在应用它,使用 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;

结果 12、23、1283,存储 1283

关于c# - 如何自定义 DataTable 列的排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8699398/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com