冒泡排序 for (int i = 0; i < listBox1.Items.Count; i++) { for (int j = i + 1; j < listBox1.Items.Count; j++) { int now =Convert.ToInt32( listBox1.Items[j-1]); int next=Convert.ToInt32( listBox1.Items[j]); if (now>next) { listBox1.Items[j-1] = next; listBox1.Items[j] = now; } } }
ListBox本身就有排序属性,不知道楼主是不是想代码排序,乱编个例子给你参考 private void button2_Click(object sender, EventArgs e) { int[] intList = new int[5] { 1, 5, 8, 22, 9 };//假设是你的原数据 listBox1.Sorted = false;//ListBox本身有排序功能,在属性中直接设置为真即可,这里是代码排序设false listBox1.Items.Clear();//清空 ArrayList arraylist = new ArrayList(); arraylist .AddRange (intList );//转为ArrayList数组 arraylist .Sort ();//排序 for (int i = 0; i < arraylist.Count; i++) { listBox1.Items.Add(arraylist[i]);//依次添加 } }
for (int i = 0; i < listBox1.Items.Count; i++)
{
for (int j = i + 1; j < listBox1.Items.Count; j++)
{
int now =Convert.ToInt32( listBox1.Items[j-1]);
int next=Convert.ToInt32( listBox1.Items[j]);
if (now>next)
{
listBox1.Items[j-1] = next;
listBox1.Items[j] = now;
}
}
}
private void button2_Click(object sender, EventArgs e)
{
int[] intList = new int[5] { 1, 5, 8, 22, 9 };//假设是你的原数据
listBox1.Sorted = false;//ListBox本身有排序功能,在属性中直接设置为真即可,这里是代码排序设false
listBox1.Items.Clear();//清空
ArrayList arraylist = new ArrayList();
arraylist .AddRange (intList );//转为ArrayList数组
arraylist .Sort ();//排序
for (int i = 0; i < arraylist.Count; i++)
{
listBox1.Items.Add(arraylist[i]);//依次添加
}
}