将数字字符串排序
时间:2011-03-22 来源:Vicky_Chen
string str = "843217965";
char[] chars = str.ToCharArray();
List<int> lists = new List<int>();
foreach (char i in chars)
{
lists.Add(int.Parse(i.ToString()));
}
//lists.Sort();//sort默认是从小到大的。显示123456789
int[] ints = lists.ToArray();
//冒泡排序,这个是从小到大显示
for (int i = 0; i < ints.Length; i++)
{
for (int j = i; j < ints.Length; j++)
{
if (ints[i] > ints[j])
{
int temp = ints[i];
ints[i] = ints[j];
ints[j] = temp;
}
}
}
for (int i = 0; i < ints.Length;i++ )
{
Console.Write(ints[i].ToString());
}
相关阅读 更多 +