简体   繁体   中英

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

        }

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

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

您正在排序lbNumbers.Text =>字符串

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM