我有一个 numbers
和 number-letter
和 letter-number
的列表。我列的类型是字符串。我的数据是像这样:
1
14
3
S-34
2
36-1/E
26
S-14
20
S-2
19
36-1
30
35
S-1
34
但我想这样排序:
1
2
3
14
20
25
30
35
36-1
36-1/E
S-1
S-2
S-14
S-34
但是我的代码是这样对数据进行排序的:
1
14
19
2
20
25
3
30
35
36-1
36-1/E
S-1
S-14
S-2
S-34
我的代码是:
List<ViewTestPackageHistorySheet> lstTestPackageHistorySheets = _reportTestPackageHistorySheetRepository.ShowReport(Id).OrderBy(i => i.JointNumber).ToList();
我更改了下面的代码但失败了。
List<ViewTestPackageHistorySheet> lstTestPackageHistorySheets = _reportTestPackageHistorySheetRepository.ShowReport(Id).OrderBy(i => Convert.ToInt32(i.JointNumber)).ToList();
错误是:
LINQ to Entities does not recognize the method 'Int32 ToInt32(System.String)' method, and this method cannot be translated into a store expression.
最佳答案
使用以下字母数字排序器,它在内部使用 PInvoke
public class AlphaNumericSorter : IComparer<string>
{
public int Compare(string x, string y)
{
return SafeNativeMethods.StrCmpLogicalW(x, y);
}
}
[SuppressUnmanagedCodeSecurity]
internal static class SafeNativeMethods
{
[DllImport("shlwapi.dll", CharSet = CharSet.Unicode)]
public static extern int StrCmpLogicalW(string psz1, string psz2);
}
用法:
List<string> testList = // Your input list;
testList.Sort(new AlphaNumericSorter());
现在 testList
包含问题中预期的排序数据
关于c# - 包含数字和字母的排序字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39011859/