简体   繁体   English

[C#]对列表框中的项目进行排序

[英][C#]Sort items in listbox

I'm using Regex to parse HTML:我正在使用正则表达式来解析 HTML:

  private void button2_Click(object sender, EventArgs e)

    {
         string html = textBox1.Text;
        foreach (Match matcha in Regex.Matches(html, @"<(.*?)>(.*?)</(.*?)>"))
        {
            listBox1.Items.Add(matcha.Index.ToString() + matcha);
        }
        foreach (Match matchb in Regex.Matches(html, @"<input type=(.*?)>"))
        {
            listBox1.Items.Add(matchb.Index.ToString() + matchb);
        }}

When button2 pressed, listbox1 has the following items:按下button2listbox1具有以下项目:

  1. 25... 25...

  2. 29.. . 29...

  3. 27.. . 27..​​.

I want to expected Output我想要预期的输出

  1. 25. .. 25 ...

  2. 27... 27..​​.

  3. 29... 29...

What should I do?我该怎么办?

Add your items to a list, sort it and then make the sorted list source of your ListBox:将您的项目添加到列表中,对其进行排序,然后制作您的 ListBox 的排序列表源:

private void button2_Click(object sender, EventArgs e)
{
    string html = textBox1.Text;
    List<String> tmp = new List<String>();//Add this
    foreach (Match matcha in Regex.Matches(html, @"<(.*?)>(.*?)</(.*?)>"))
    {
        tmp.Add(matcha.Index.ToString() + matcha);//Change this line
    }
    foreach (Match matchb in Regex.Matches(html, @"<input type=(.*?)>"))
    {
        tmp.Add(matchb.Index.ToString() + matchb);//Change this one too
    }

    var sorted = list.OrderBy(x => PadNumbers(x));//Add this line 
    listBox1.Datasource = sorted;//and this
}

when PadNumbers are defined as:PadNumbers定义为:

public static string PadNumbers(string input)
{
    return Regex.Replace(input, "[0-9]+", match => match.Value.PadLeft(10, '0'));
}

This should sort all nubmers as "natural sort" would.这应该像“自然排序”那样对所有数字进行排序。 So if your numbers will be 8, 90, 10 , this script will sort it as 8, 10, 90 , while normal sort would return 10, 8, 90 .因此,如果您的数字是8, 90, 10 ,则此脚本会将其排序为8, 10, 90 ,而正常排序将返回10, 8, 90
Is all clear?都清楚了吗?

在添加项目之前将 sorted 属性设置为 true:

listBox1.sorted = true;
 private void button1_Click(object sender, EventArgs e)
    {

        int num_items = listBox1.Items.Count;
        object[] items = new object[num_items];
        listBox1.Items.CopyTo(items, 0);

        // Sort them by their contained numeric values.
        items = SortNumericItems(items);

        // Display the results.
        listBox1.Sorted = false;
        listBox1.DataSource = items;
    }

 private object[] SortNumericItems(object[] items)
    {
        // Get the numeric values of the items.
        int num_items = items.Length;
        const string float_pattern = @"-?\d+\.?\d*";
        double[] values = new double[num_items];
        for (int i = 0; i < num_items; i++)
        {
            string match = Regex.Match(
                items[i].ToString(), float_pattern).Value;
            double value;
            if (!double.TryParse(match, out value))
                value = double.MinValue;
            values[i] = value;
        }

        // Sort the items array using the keys to determine order.
        Array.Sort(values, items);
        return items;
    }

When button1 pressed, listbox1 has the following items:按下 button1 时,listbox1 具有以下项目:

  1. 25 25
  2. 28 28
  3. 20 20

output display below输出显示如下在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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