排序问题
描述: 现有一个需求,需要对List里面的Item进行排序,但是使用sort()方法排序的话,如果集合里是字符串的话可以排序成功,但如果是数字的话就无法达到预期的目的。
例如,集合是这样的:
strList = new List() {
"1","10","11","12","13","14","15","16",
"17","18","19","2","20","21","22","23",
"3","4","5","6","7","8","9"
};
使用sort()排序后是这样的顺序:1,10,11,12,13,14,15,16,17,18,19,2,20,21,22,23,3,4,5,6,7,8,9
而我想要的顺序是:1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23
请问:该如何实现?
解决方案1:using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
List strList = new List() { "1", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "2", "20", "21", "22", "23", "3", "4", "5", "6", "7", "8", "9" };
List intList = new List();
for (int i = 0; i < strList.Count; i++)
{
intList.Add(Convert.ToInt32(strList[i]));
}
for (int i = 0; i < intList.Count; i++)
{
for (int j = 0; i < intList.Count - j - 1; j++)
{
int Temp;
if (intList[j] > intList[j+1])
{
Temp = intList[j];
intList[j] = intList[j + 1];
intList[j + 1] = Temp;
}
}
}
string str = "";
for (int i = 0; i < intList.Count; i++)
{
str += intList[i].ToString() + ",";
}
Console.Write(str.TrimEnd(','));
Console.ReadLine();
}
}
}
解决方案2:List strList = new List(new string[] { "1", "10", "11", "12", "abs", "wds", "cfs", "bfe", "ytd", "xwz", "pjg", "itf", "13", "14", "15", "16", "17", "18", "19", "2", "20", "21", "22", "23", "3", "4", "5", "6", "7", "8", "9" });
strList.Sort(delegate(string a, string b)
{
string ia = Regex.Match(a, @"\d+$").Value;
string ib = Regex.Match(b, @"\d+$").Value;
return (ia != "" && ib != "") ? int.Parse(a) - int.Parse(b) : string.Compare(a, b);
});
解决方案3:
List values = new List(new String[]{
"1", "10", "11", "12", "13", "14", "15", "16",
"17", "18", "19", "2", "20", "21", "22", "23",
"3", "4", "5", "6", "7", "8", "9"});
values.Sort(delegate(String value1, String value2) {return Int32.Parse(value1).CompareTo(Int32.Parse(value2)); });
//直接用delegate吧,如果这个IComparer不在别的地方使用
解决方案4:List strList = new List(new string[] { "1", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "2", "20", "21", "22", "23", "3", "4", "5", "6", "7", "8", "9" });
strList.Sort((Comparison)delegate(string a, string b)
{
return int.Parse(a).CompareTo(int.Parse(b));
});
strList 就是按你要求排序后的。vs2005起支持以上语法。更高级的vs可以用linq
解决方案5:List strList = new List() { "1", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "2", "20", "21", "22", "23", "3", "4", "5", "6", "7", "8", "9" };
strList.Sort( delegate(string str1, string str2) { return int.Parse(str1) - int.Parse(str2); } );
解决方案6: static void Main(string[] args)
{
List strList = new List() { "1", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "2", "20", "21", "22", "23", "3", "4", "5", "6", "7", "8", "9" };
strList.Sort( new Comp());
foreach (string s in strList)
{
Console.WriteLine(s);
}
Console.ReadKey();
}
private class Comp : Comparer
{
public override int Compare(string x, string y)
{
return int.Parse(x) - int.Parse(y);
}
}
以上介绍了“lt List排序问题”的问题解答,希望对有需要的网友有所帮助。
本文网址链接:http://www.codes51.com/itwd/2419759.html