0

Im trying to sort a listbox full of numbers numerically. Why doesnt this work?

        {
            ArrayList Sorting = new ArrayList();
            Sorting.Add (lbNumbers.Text);
            int[] items = new int[Sorting.Count];
            Sorting.CopyTo(items);
            Array.Sort(items);
            lbNumbers.Items.Add(items);

        }
Furqan Safdar
  • 16,260
  • 13
  • 59
  • 93
Simon Roberts
  • 21
  • 1
  • 5

7 Answers7

2

Probably because when your numbers are represented as strings, they will not sort the way you expect. They will sort as strings and not as numbers.

For example, if you had a list such as:

10
9
101

It would be sorted as:

10
101
9
Randy Minder
  • 47,200
  • 49
  • 204
  • 358
1

First, parse the string-elements, then sort.

// the itemList is your lbNumbers.Text
var itemList = new List<string> {"9", "1", "10", "11"};

// use TryParse if you're not sure if really all elements are numbers
var numberList = itemList.Select(int.Parse).ToList();
numberList.Sort();
anscheinbar
  • 288
  • 1
  • 6
0
ArrayList Sorting = new ArrayList(); 

foreach (var o in listBox1.Items) {
    Sorting.Add(o);
} 

Sorting.Sort(); 

listBox1.Items.Clear();

foreach (var o in Sorting) {
    listBox1.Items.Add(o); 
}

ADDED: For sort in descending order,

1.Create a class ReverseSort as shown below:

// implementation:
public class ReverseSort : IComparer
{
    public int Compare(object x, object y)
    {
        // reverse the arguments
        return Comparer.Default.Compare(y, x);
    }    
}

2.Replace the code line of Sort with this line:

Sorting.Sort(new ReverseSort());
Furqan Safdar
  • 16,260
  • 13
  • 59
  • 93
  • nevermind this code works. Thank you. What do I add to make it sort the opposite direction? Ascending instead of Decending? – Simon Roberts Oct 09 '12 at 13:12
0

You are sorting lbNumbers.Text => strings

Felipe Ardila
  • 2,995
  • 2
  • 14
  • 12
0

You must clear before sorting

ArrayList arrayList = new ArrayList(); 
foreach (object o in lbNumbers.Items) 
{
   arrayList.Add(o);
}

arrayList.Sort(); 

lbNumbers.Items.Clear();

foreach(object o in arrayList)
{
    lbNumbers.Items.Add(o); 
}
Aghilas Yakoub
  • 28,516
  • 5
  • 46
  • 51
0

Using a little bit of LINQ

        string list = "1,24,3,10,12,11";

        //Split the string into the tokens containing the numbers
        string[] tokens = list.Split(',');

        //Parse each string representing an integer into an integer
        //return the resultant object as an array of integers
        int[] sorting = tokens.Select(x => int.Parse(x)).ToArray<int>();

        //Sort them numerically and return as an array of integers
        sorting = sorting.OrderBy(x => x).ToArray<int>();

        //Display them to convince ourselves it works.
        foreach (int x in sorting)
        {
            Console.WriteLine(x);
        }

        Console.ReadLine();

The parsing and ordering can be done in the same statement, but were split out here for ease of reading.

Colin Desmond
  • 4,824
  • 4
  • 46
  • 67
-1

Try this example:



    listBox1.Items.Add(3);
    listBox1.Items.Add(1);
    listBox1.Items.Add(2);

    ArrayList sort = new ArrayList();
    foreach (object item in listBox1.Items)
    {
        sort.Add(item);
    }
    sort.Sort();
    listBox1.Items.Clear();
    foreach (int item in sort)
    {
        listBox1.Items.Add(item);
    }

What you where trying to do was to read only the selected text. This way you can get all items in the listbox to an arraylist, sort them and then adding them back again to the listbox.

Keep in mind that all the unsorted items are still there so you need to clear the listbox first. Thats what the listBox1.Items.Clear(); does

Yaroslav
  • 6,476
  • 10
  • 48
  • 89
Pedro Costa
  • 175
  • 2
  • 11