0

I'm using Regex to parse 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:

  1. 25...

  2. 29...

  3. 27...

I want to expected Output

  1. 25...

  2. 27...

  3. 29...

What should I do?

Malakiya sanjay
  • 208
  • 2
  • 12
LHVinh
  • 3
  • 1
  • You should add the matching items to a temporary collection, sort it, and then add the items of that collection to your listbox. Be aware that you might have more things to consider, for instance, how would you want the following 3 items to be sorted? `8, 90, 10`, I would assume you want them as `8, 10, 90`, but the normal textual sort would be `10, 8, 90`, because it sorts them as text and not as numbers. You need a "natural sort" to handle this case. ie. text sort means `1 < 8 < 9` – Lasse V. Karlsen Feb 07 '20 at 09:52
  • Does this answer your question? [Natural Sort Order in C#](https://stackoverflow.com/questions/248603/natural-sort-order-in-c-sharp) – xdtTransform Feb 07 '20 at 09:56
  • Does this answer your question? http://csharphelper.com/blog/2016/04/sort-listbox-or_combobox-items-in-numeric-order-in-c/ – Malakiya sanjay Feb 07 '20 at 10:05

3 Answers3

1

Add your items to a list, sort it and then make the sorted list source of your 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:

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.
Is all clear?

Encyklopedie
  • 111
  • 1
  • 6
0

set sorted property true before you add items:

listBox1.sorted = true;
Micha
  • 906
  • 6
  • 9
0
 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:

  1. 25
  2. 28
  3. 20

output display below enter image description here

Malakiya sanjay
  • 208
  • 2
  • 12