ListBox里面有一些String 下面是我的算法private void RandomPut()
        {
            Random rd = new Random();
            string Obj;
            progressBar.MaxNum=listBox.Items.Count;
            progressBar.value=0;
            for(int n=0;n<listBox.Items.Count;n++)
            {
                Obj = listBox.Items[n].ToString();
                listBox.Items.RemoveAt(n);
                listBox.Items.Insert(rd.Next(listBox.Items.Count), Obj);
                progressBar.PerformStep();
            }
        }当listBox.Items.Count<20000 的时候 尚可接受
但是如果listBox.Items.Count>100000 进行的速度足以让任何人抓狂...
求高人指点  如何更快速的乱序

解决方案 »

  1.   

    Insert into an array is a slow operation. Plus, the listbox needs to draw all items after an insert.1. You can disable instant update by calling listBox.BeginUpdate() to speed up (see the following code)
    2. In terms of data structure, you might change insert to swap (you need to write it yourself)
    private void RandomPut()
    {
                Random rd = new Random();
                string Obj;
                progressBar.MaxNum=listBox.Items.Count;
                progressBar.value=0;            listBox.BeginUpdate();                       //<---
                for(int n=0;n<listBox.Items.Count;n++)
                {
                    Obj = listBox.Items[n].ToString();
                    listBox.Items.RemoveAt(n);
                    listBox.Items.Insert(rd.Next(listBox.Items.Count), Obj);
                    progressBar.PerformStep();
                }
                listBox.EndUpdate();                         //<---
    }
      

  2.   

    Coulde it be faster?   I'll try thisRandomPut() IS the exactly sub-Thread-MethordThanks..  Both of YOU
      

  3.   

    Not so effective in speeding up期待其他的解决方法
      

  4.   

    代码有bug,如果Items总共1000个,某一次插入操作假设插入位置在146,那么当你的循环变量n到146的时候,就会把这个数字再次随机找个位置插入。
    最后结果有的数字没有被移位,有的数字被随机移位N次。
    再说了你的代码包含RemoveAt的删除方法,好像比较好的习惯应该是循环从最高位到最低位吧,你的循环变量从0开始走高,好像有潜在的bug危险,虽然你的现在这个程序估计没问题。
      

  5.   

    这个速度比较快,1-2秒就搞定了。
    button1是加入数据,button2是乱序排列using System;
    using System.Collections.Generic;
    using System.Windows.Forms;namespace Test
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        delegate void dele();        private void button1_Click(object sender, EventArgs e)
            {
                listBox1.BeginUpdate();
                for(int i = 0;i < 100000;i++)
                    listBox1.Items.Add(i);
                listBox1.EndUpdate();
            }        private void button2_Click(object sender, EventArgs e)
            {
                List<object> list = new List<object>();
                foreach(object item in listBox1.Items)
                    list.Add(item);
                list.Sort(new Cmp());
                listBox1.BeginUpdate();
                listBox1.Items.Clear();
                foreach(object item in list)
                    listBox1.Items.Add(item);
                listBox1.EndUpdate();
            }
        }
        class Cmp : IComparer<object>
        {
            Random rnd = new Random();
            #region IComparer<object> 成员        public int Compare(object x, object y)
            {
                return rnd.Next(3) - 1;
            }        #endregion
        }
    }
      

  6.   

    谢谢楼上的  刚刚百度了一下IComparer的原型return rnd.Next(3) - 1;  
    返回 -1 0 1  分别表示 x<y  x=y  x>y 吧明天在Form上试试这个效率怎样
      

  7.   

    OK果然快了不少  给分   3Q
      

  8.   

    但是第2次或者第3次 button2_Click 的时候会出现这样的异常当 Array.Sort 调用 x.CompareTo(x) 时,IComparer (或其依赖的 IComparable 方法)未返回零。x:“13000090035”x 的类型:“String”IComparer:“NameSpace.Cmp”。why..?
      

  9.   

    对于此问题,我的解决方法是
    先声明全局变量        Cmp cmp = new Cmp();
    再用
                list.Sort(cmp);
      

  10.   

    谢谢回复!!
    我就是这样做的  但是问题依旧...
    事实上 是这样做的Cmp cp = new Cmp();
    foreach (string item in listBox.Items)
    {
         list.Add(item);
         progressBar1.PerformStep();
    }
    try
      {
          list.Sort(cp);
       }
    catch
    {
    }加了try catch以后 不处理这个异常 也就是只进行了部分排序
      

  11.   

    是因为当对同一个对象进行比较时,应该返回相等,而没有返回相等。简单改一下即可public class RandomCompare<T> : IComparer<T>
        {
            static Random rnd = new Random();
            #region IComparer<object> 成员        public int Compare(T x, T y)
            {
                if (x.Equals(y))
                    return 0;
                return rnd.Next(3) - 1;
            }        #endregion
        }