本文介绍了C#按给定订单排序列表框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!



我有一个问题,我无法理解,谷歌也不想帮助我:



我正在填写这样的列表框:



Hi everyone!

I have a problem where i can't get my head around, google didn't want to help me either:

I'm filling a listbox like this:

for (int i = 0; i < listbox1.Items.Count; i++)
{
    for (int j = 0; j < listbox1.Items.Count; j++)
    {
        if (i < j)
        {
            if (generator.Next(0, 2) == 0)
            {
                listbox2.Items.Add(listbox1.Items[i] + " : " + listbox1.Items[j]);

            }
            else
            {
                listbox2.Items.Add(listbox1.Items[j] + " : " + listbox1.Items[i]);
            }
        }
    }
}





在我的案例结果在listbox2中有6个项目(给定listbox1中有4个项目)。

现在我想按给定的顺序对项目进行排序,即:1,3,5, 6,4,2



可视化我想用listbox2获得的地方:



Item1 - > Item1

Item2 - > Item3

Item3 - > Item5

Item4 - > Item6

Item5 - > Item4

Item6 - > Item2



但是我觉得明确的顺序不应该那么重要(或者是它?),如果有一般的方法,我会更感兴趣标记我的项目有一个值,按升序排序......



我知道如何实现它?



which in my case results in listbox2 with 6 items in it (given listbox1 has 4 items in it).
Now i'd like to sort the items by a given order, which is: 1,3,5,6,4,2

Visualization of where i want to get with listbox2:

Item1 --> Item1
Item2 --> Item3
Item3 --> Item5
Item4 --> Item6
Item5 --> Item4
Item6 --> Item2

But i guess the explicit order shouldn't be that important (or is it?), i'd be more interested in if there's a general way of "tagging" my items with a value to ascendingly sort them by...

Any idea how i can implement that?

推荐答案

private void button1_Click(object sender, EventArgs e)
{
    int[] array = { 4, 5, 8, 2, 6, 9, 1, 7 };

    int[] arrayOdds = new int[array.Length];
    int[] arrayPairs = new int[array.Length];
    List<int> newArray = new List<int>();

    //separate Odds from pairs
    for (int i = 0; i < array.Length; i++)
    {
        if (array[i] % 2 == 1)
        {
            arrayOdds[i] = array[i];
        }
        else
        {
            arrayPairs[i] = array[i];
        }
    }

    //bubble sort
    for (int i = 0; i < array.Length; i++)
    {
        for (int j = 0; j < array.Length - 1; j++)
        {
            //bubble sort odds ascending
            if (arrayOdds[j] > arrayOdds[j + 1])
            {
                int aux = arrayOdds[j];
                arrayOdds[j] = arrayOdds[j + 1];
                arrayOdds[j + 1] = aux;
            }

            //bubble sort pairs descending
            if (arrayPairs[j] < arrayPairs[j + 1])
            {
                int aux = arrayPairs[j];
                arrayPairs[j] = arrayPairs[j + 1];
                arrayPairs[j + 1] = aux;
            }
        }

    }

    //get results together on a list
    for (int i = 0; i < array.Length; i++)
    {
        if (arrayOdds[i] > 0)
            newArray.Add(arrayOdds[i]);
    }
    for (int i = 0; i < array.Length; i++)
    {
        if (arrayPairs[i] > 0)
            newArray.Add(arrayPairs[i]);
    }

}


int[] order = new int[] { 1, 3, 5, 6, 4, 2 };
string[] sort = new string[12];
int f = 0;
foreach (object o in listbox2.Items)
{
    sort[order[f]] = Convert.ToString(o);
    f++;
}
listbox2.Items.Clear();
foreach (object o in sort)
{
    if (o != null)
    {
        listbox2.Items.Add(o);
    }
}


这篇关于C#按给定订单排序列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 12:32
查看更多