我有一个很好的长订单ID列表,后跟csv文件中的序列号(OID,SN)。我需要按序列号对列表进行排序,它使用base58,所以大写字母在小写字母之前。
Base58:123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz
刚做
IEnumerable<string> query =
from line in serialNumbers
let x = line.split(',')
orderby x[1]
select x[0] + "," + x[1];
string[] temp = query.ToArray();
按字母顺序排序,但不按Base58顺序排序。我怎么做到这一点?使用C#。
答案 0 :(得分:0)
您可以定义自定义比较器,例如:
class Base58Comparer : IComparer<string>
{
public int Compare(string x, string y)
{
if (object.ReferenceEquals(x, y))
return 0;
if (object.ReferenceEquals(x, null))
return -1;
if (object.ReferenceEquals(y, null))
return 1;
int minLen = Math.Min(x.Length, y.Length);
for (int i = 0; i < minLen; i++)
{
// comparison based on ASCII value
int comp = x[i].CompareTo(y[i]);
if (comp != 0)
return comp;
}
return x.Length - y.Length;
}
}
然后您可以将其传递给OrderBy
方法,例如:
var query = serialNumbers.Select(line => line.Split(','))
.OrderBy(x => x[1], new Base58Comparer())
.Select(x => x[0] + "," + x[1]);