排序具有特殊字符的datatable字段 [英] sorting a field of datatable which have special character
问题描述
我有一个 datatable
,其中有一些列已经从访问数据库中的一个表中填入。我有一个专栏是SN,其中包含如下数据:
I have a datatable
which have some columns which I have populated from a table in a access database. One special column that I have is SN which consists data like this:
SN
_____
-1
1.a
1.a
1.b
10
1000
1001
1002
11.a
11.b
13.a
2.a
2.b
2.c
2000
2001
21.a
22.b
3.a
3.b
3.c
4000
4001
4.a
SN字段是字符串格式,当我对数据进行排序时,结果如图所示
但是我喜欢这里是排序的列,我希望是这样的:
The SN field is in string format and when I sort the data the result is as shown above.
But what I like to have here is the sorted column which I am hoping to be something like this:
SN
_____
-1
1.a
1.a
1.b
2.a
2.b
2.c
3.a
3.b
3.c
4.a
10
11.a
11.b
13.a
21.a
22.b
1000
1001
1002
2000
2001
4000
4001
可以做些什么来实现这个?
What can be done to achieve this?
更新:为了清楚起见,我添加了不多的数据。在实施建议的代码之后,我获得的结果是:
UPDATE: I have added nore data for clarity. The result I acquired after implementing the suggested code is:
SN
_____
-1
1.a
1.a
1.b
10
11.a
11.b
13.a
2.a
2.b
2.c
21.a
22.b
3.a
3.b
3.c
4.a
1000
1001
1002
2000
2001
4000
4001
推荐答案
您可以使用可变功能对数据进行排序。这有点痛苦,但可以尝试以下代码吗?
You could sort your datatable using enumarable features. It's a bit painful but could you try the code below ?
DataTable myDataTable = new DataTable();
myDataTable.Columns.Add("SN", typeof(string));
//string myValues = "-1;1.a;1.b;10;1000;1001;1002;2.c;2.b;2.a;2000;2001;3.a;3.b;3.c;4000;4001;4.a;1.a";
string myValues = "a;a.b;c.c;-1;1.a;1.a;1.b;10;11.a;11.b;13.a;2.a;2.b;2.c;21.a;22.b;3.a;3.b;3.c;4.a;1000;1001;1002;2000;2001;4000;4001";
string[] myValuesArray = myValues.Split(';');
foreach (string myValue in myValuesArray)
{
DataRow myRow = myDataTable.NewRow();
myRow["SN"] = myValue;
myDataTable.Rows.Add(myRow);
}
string beforeSort = string.Join(";", myDataTable.AsEnumerable().Select(x => x["SN"]));
Console.WriteLine("Before Sorting:");
Console.WriteLine(beforeSort);
IEnumerable<DataRow> sortedValues = myDataTable.AsEnumerable()
.OrderBy(x =>
{
string currentStringValue = x["SN"].ToString();
string[] currentStringValueArray = currentStringValue.Split('.');
if (currentStringValueArray.Length == 2)
{
string currentPart = "";
int currentPartNumeric = 0;
if (int.TryParse(currentStringValueArray[0], out currentPartNumeric))
{
currentPart += currentPartNumeric.ToString();
}
else
{
//We are assuming our alphanumeric chars are integers
currentPart += (((int)(char.ToUpper(char.Parse(currentStringValueArray[0])))) - 64).ToString();
}
if (int.TryParse(currentStringValueArray[1], out currentPartNumeric))
{
currentPart += "." + currentPartNumeric.ToString();
}
else
{
//We are assuming our alphanumeric chars are integers
currentPart += "." + (((int)(char.ToUpper(char.Parse(currentStringValueArray[1])))) - 64).ToString();
}
return Convert.ToDecimal(currentPart, CultureInfo.InvariantCulture);
}
else if (currentStringValueArray.Length == 1)
{
int currentPartNumeric = 0;
string currentPart = "";
if (int.TryParse(currentStringValueArray[0], out currentPartNumeric))
{
currentPart += currentPartNumeric.ToString();
}
else
{
//We are assuming our alphanumeric chars are integers
currentPart += "." + (((int)(char.ToUpper(char.Parse(currentStringValueArray[0])))) - 64).ToString();
}
return Convert.ToDecimal(currentPart, CultureInfo.InvariantCulture);
}
else
return 0m;
});
string afterSort = string.Join(";", sortedValues.Select(x => x["SN"]));
Console.WriteLine("After Sorting:");
Console.WriteLine(afterSort);
//Copy to your existing datatable
myDataTable = sortedValues.CopyToDataTable();
PS:由于确保代码稳定性,我混合了您的现有数据
PS: I mixed your existing data due being sure about the code stability
更新:这只是您的数据的一个例子,请记住,您应该根据您的数据多样性修改OrderBy范围。这个例子只适用于x.y和x格式的数据。
Update: This is just an example to your data, please keep in mind you should modify the OrderBy scope according to your data diversity. That example will work only for x.y and x format of data.
希望这有助于
这篇关于排序具有特殊字符的datatable字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!