日期:2014-05-17  浏览次数:20849 次

这个集合如何自定义排序
List<string> list = new List(){
"12","09","10","32","25","01","05","24","15","17","08","05","07","20","21","22","30","03","19"}


要求按照数字大小排序:01、02、03、04、05、06、07、08、09、10、11..............
------解决方案--------------------
list.Sort((x,y)=> int.Parse(x).CompareTo(int.Parse(y)));

------解决方案--------------------
List<string> list = new List(){
"12","09","10","32","25","01","05","24","15","17","08","05","07","20","21","22","30","03","19"};
List<string> result = list.OrderBy(x => int.Parse(x)).ToList();
------解决方案--------------------
引用:
引用:List<string> list = new List(){
"12","09","10","32","25","01","05","24","15","17","08","05","07","20","21","22","30","03","19"};
List<string> result = list.OrderBy(x =……

可以。
其实,如果开头有0补齐的,反倒不用Parse
List<string> result = list.OrderBy(x => x).ToList(); 就可以了。
------解决方案--------------------

            List<string> list = new List<string>{
 "12","09","10","32","25","01","05","24","15","17","08","05","07","20","21","22","30","03","19"};

            List<string> result = list.OrderBy(c => int.Parse(c)).ToList();
            result.ForEach(c => Console.WriteLine(c));
 

------解决方案--------------------
效果如下图;

------解决方案--------------------
直接 调用list.Sort();就可以了。
因为string的比较是基于char的编码值进行比较的。


------解决方案--------------------
 List<string> list = new List<string> { "12", "09", "10", "32", "25", "01", "05", "24", "15", "17", "08", "05", "07", "20", "21", "22", "30", "03", "19" };
            var list1 = list.OrderBy(x => x).ToList();
            var list2 = list.OrderBy(x => int.Parse(x)).ToList();
            list1.ForEach(x => Console.WriteLine(x));
            list2.ForEach(x => Console.WriteLine(x));