Simon Roberts
Simon Roberts

Reputation: 21

Sort ListBox numerically in C#

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);

        }

Upvotes: 0

Views: 7919

Answers (7)

Pedro Costa
Pedro Costa

Reputation: 195

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

Upvotes: -1

Furqan Safdar
Furqan Safdar

Reputation: 16708

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());

Upvotes: 0

Colin Desmond
Colin Desmond

Reputation: 4854

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.

Upvotes: 0

anscheinbar
anscheinbar

Reputation: 288

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();

Upvotes: 1

Aghilas Yakoub
Aghilas Yakoub

Reputation: 28980

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); 
}

Upvotes: 0

Felipe Ardila
Felipe Ardila

Reputation: 3004

You are sorting lbNumbers.Text => strings

Upvotes: 0

Randy Minder
Randy Minder

Reputation: 48442

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

Upvotes: 2

Related Questions