混合字母和数字的字符串字母排序
本帖最后由 asdfg123_ 于 2013-12-15 20:56:27 编辑
要将一组数据导入到Excel中,一个键对应一个值,键和值的内容都要导入到Excel中,但是利用SortedDictionary排序后得出的键排序后的结果你不是想要的。应该是先按字母排序,再按数字排序,但是SortedDictionary自动排序得出的结果中按数字排序得出的结果不是想要的,例如想要的是"A10","A11","A01",而SortedDictionary的排序结果却为"A10","A101","A11"。
又如下面:
数据有:"A01", "A02", "A22", "B02", "A23-1", "A23", "A222", "A101", "A10-1", "A11"
想要的排序结果是:"A01", "A02", "A10-1", "A11", "A22", "A23", "A23-1", "A101", "A222", "B02"
而现在的结果是:"A01", "A02", "A10-1", "A101", "A11", "A22", "A222", "A23", "A23-1", "B02"
这该怎么办?
------解决方案--------------------学习与参考下面的方法:
http://www.cnblogs.com/insus/category/508018.html
------解决方案--------------------
List<string> lst = new string[] { "A01", "A02", "A22", "B02", "A23-1", "A23", "A222", "A101", "A10-1", "A11" }.ToList<string>();
var query = from t in lst
orderby t.Substring(0, 1) + t.Substring(1).Substring(0, t.Contains('-') ? t.IndexOf('-') - 1 : t.Length - 1).PadLeft(5) + (t.Contains('-') ?"-":"")
select t;
List<string> s = query.ToList<string>();
------解决方案--------------------
呵呵,那里的排序就是