Sort ListBox numerically in C#

和自甴很熟 提交于 2019-12-10 11:59:50

问题


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

        }

回答1:


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



回答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



回答3:


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



回答4:


You are sorting lbNumbers.Text => strings




回答5:


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



回答6:


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.




回答7:


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



来源:https://stackoverflow.com/questions/12800461/sort-listbox-numerically-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!