Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ListBox Sort
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 10:01am
Edited 3-Jul-14 11:00am
v5
Comments
goathik at 3-Jul-14 15:19pm
   
is that ascending odds and then descending pairs?
Member 10922982 at 3-Jul-14 15:24pm
   
Yes, it is.
Rate this: bad
good
Please Sign up or sign in to vote.

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)
            newArray.Add(arrayOdds[i]);
    }
    for (int i = 0; i < array.Length; i++)
    {
        if (arrayPairs[i] > 0)
            newArray.Add(arrayPairs[i]);
    }
 
}
  Permalink  
v2
Comments
Icepatch at 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: bad
good
Please Sign up or sign in to vote.

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)
                {
                    listbox2.Items.Add(o);
                }
            }
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 7,903
1 Sergey Alexandrovich Kryukov 7,142
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,820


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 3 Jul 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100