12,406,687 members (59,839 online)
Rate this:
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:

```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 3-Jul-14 9:01am
Updated 3-Jul-14 10:00am
v5
goathik 3-Jul-14 15:19pm

is that ascending odds and then descending pairs?
Member 10922982 3-Jul-14 15:24pm

Yes, it is.

Rate this:

## Solution 1

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:

```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)
}
for (int i = 0; i < array.Length; i++)
{
if (arrayPairs[i] > 0)
}

}```
v2
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.
Rate this:

## Solution 2

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!

```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)
{
}
}
```

Top Experts
Last 24hrsThis month
 OriginalGriff 270 Karthik Bangalore 204 Richard MacCutchan 180 BillWoodruff 85 Afzaal Ahmad Zeeshan 75
 OriginalGriff 7,957 Karthik Bangalore 3,531 ppolymorphe 3,400 Richard MacCutchan 2,612 F-ES Sitecore 2,300