Click here to Skip to main content
15,860,972 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C#
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]);
            }
        }
    }
}


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?
Posted
Updated 3-Jul-14 10:00am
v5
Comments
goathik 3-Jul-14 15:19pm    
is that ascending odds and then descending pairs?
Icepatch 3-Jul-14 15:24pm    
Yes, it is.

Here is how i sorted the way you asked to,
It is a little long, but at least you can read the idea much easily.
I hope this helps:

C#
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]);
    }

}
 
Share this answer
 
v2
Comments
Icepatch 3-Jul-14 16:37pm    
I am very thankful for the effort you put into that, but i have no idea how to use your code to solve my problem. I edited my question half an hour ago to clarify my goal. It's not about the sort algorithm itself, but about a way to sort the items of my listbox other than just ascending or descending but in a given order (which just happens to be elements with an odd index ascending and elements with an even index descending). Even an answer like "C# doesn't support what you want to do, at least not with a huge load of code!" would satisfy me, since i'd only like to know if there's a "rather simple" solution which i just don't know and didn't find.
I worked out a solution for my Problem, it's dirty code, and I seem to have a logical issue in there because one used array needs to be twice as big as anticipated (to not run into an exception, where it says, that I'm outside of a defined index) and generates half the times nulls, but it works somehow! Any correction to my flaw is higly appreciated!

C#
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);
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900