Listbox data sorting C#

Listbox data sorting C#

k005
Advisor Advisor
2,218 Views
13 Replies
Message 1 of 14

Listbox data sorting C#

k005
Advisor
Advisor

 

Hello

 

How can I sort the data in the listbox from the smallest diameter to the largest?

 

sample:
listbox content:
1 25ø16 / 15 L = 300
2 30ø16 L = 620
3 14ø20 L = 330
4 80ø16 / 15 L = 429
5 120ø8 L = 181
6 25ø10 L = 140
7 25ø20 L = 561
8 25ø20 L = 398
9 15ø10 L = 140
----------------------------


the desired listbox content:
5 120ø8 L = 181
6 25ø10 L = 140
9 15ø10 L = 140
1 25ø16 / 15 L = 300
4 80ø16 / 15 L = 429
2 30ø16 L = 620
3 14ø20 L = 330
7 25ø20 L = 561
8 25ø20 L = 398


that is, our reference is the two-digit or one-digit value to ø and its right.

Thanks.

0 Likes
Accepted solutions (2)
2,219 Views
13 Replies
Replies (13)
Message 2 of 14

_gile
Consultant
Consultant

Hi,

You can use Regex ans Linq.

using System.Linq;
using System.Text.RegularExpressions;

    // ...
    list.OrderBy(s => int.Parse(Regex.Match(s, @"ø(\d+)").Groups[1].Value))

 

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 3 of 14

k005
Advisor
Advisor

Hi @_gile 

 

Is it that way?

 

 

 private void btnSirala_Click(object sender, EventArgs e)
        {
            listBox1.OrderBy(s => int.Parse(Regex.Match(s, @"ø(\d+)").Groups[1].Value))
        }

 

0 Likes
Message 4 of 14

_gile
Consultant
Consultant

@k005  a écrit :

Hi @_gile 

 

Is it that way?

 

 

 private void btnSirala_Click(object sender, EventArgs e)
        {
            listBox1.OrderBy(s => int.Parse(Regex.Match(s, @"ø(\d+)").Groups[1].Value))
        }

 


I hope not.
What is listbox1? I guess an instance of System.Windows.Forms.ListBox. If I'm right, the ListBox class does not implement IEnumerable<string>.

How do you get this collection of strings? What is the type of this collection ? Does this type implements IEnumerable<string> ?  This is mandatory to use the Linq extension methods.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 5 of 14

k005
Advisor
Advisor

@_gile 

 

I get data from another button into the listbox.

 

related code:

 

 
                List<string> allText = new List<string>();
                using (var ts = Adb.HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction())
                {
                    Type type;

                    foreach (var id in allTextIDs)
                    {

                        var dbObj = id.GetObject(Adb.OpenMode.ForRead);
                        type = dbObj.GetType();
                        if (type.Equals(typeof(Adb.DBText)))
                        {

                            if (chcbPoz.Checked)

 

But:

 

I want to do this on another button. the reason is I need both lists ...

 

In other words, after getting into the listbox, we have to sort with the "btnSirala" button.

0 Likes
Message 6 of 14

_gile
Consultant
Consultant

Sorry I do not understand what you're trying to achieve.

You just have to learn/undertsand that

  • you can use the OrderBy Linq extension with objects which type implements IEnumerable<string> (as List<string> or string[])
  • you can fill the ListBox.Items with this kind of objects
  • The ListBox.Items type is ObjectCollection and can be casted into an IEnumerable<<string> using ListBox.Items.Cast<stirng>()


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 7 of 14

k005
Advisor
Advisor

@_gile 

 

OK. The situation is a little confused for me.

I am unfamiliar with these terms for the time being.

 

But:

 

I just want to sort the data in the current listbox. According to the reference I mentioned. I want to do this with the button called "btnSirala". Is this too complicated?

 

The current listbox view is attached.

0 Likes
Message 8 of 14

_gile
Consultant
Consultant
Accepted solution

You have to get the items from the list box, cast the collection into an IEnumerable<string>, sort it with OrderBy, clear the list box items and re-fill it with the sorted IEnumerable<string>.

        private void btnSirala_Click(object sender, EventArgs e)
        {
            var items = listBox1.Items
                .Cast<string>()
                .OrderBy(s => int.Parse(Regex.Match(s, @"ø(\d+)").Groups[1].Value))
                .ToArray();
            listBox1.Items.Clear();
            listBox1.Items.AddRange(items);
        }

You won't learn anything if you're still waiting for someone to write the code for you. These are the basics of Windows forms that have nothing to do with the AutoCAD .NET API.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 9 of 14

k005
Advisor
Advisor

@_gile 

 

//
You won't learn anything if you're still waiting for someone to write the code for you. These are the basics of Windows forms that have nothing to do with the AutoCAD .NET API.
//

 

 

First of all, let's make one thing clear:

 

I have been writing questions on the Forum for about 3-4 months. and I learned a lot. Yes, there are shortcomings.

 

I'm trying to complete.

 

For me, I don't expect anyone to write the code, including you. I'm trying to learn from the Answers given.

 

If this is a problem for you and anyone else, you may not be able to write an answer.

 

* Also, the solution with the last code you sent has not been approved by me yet. It gives an error.

 

 

 

 

0 Likes
Message 10 of 14

_gile
Consultant
Consultant
Accepted solution

You didn't say you use a DataSource for the list box as said in the error message. If so, try:

        private void btnSirala_Click(object sender, EventArgs e)
        {
            listBox1.DataSource = ((IList)listBox1.DataSource)
                .Cast<string>()
                .OrderBy(s => int.Parse(Regex.Match(s, @"ø(\d+)").Groups[1].Value))
                .ToArray();
        }

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 11 of 14

k005
Advisor
Advisor

@_gile 

 

 

Thank you so much. All right. 🤗

* I just added a little code ...

 

listBox1.DataSource = ((System.Collections.IList)listBox1.DataSource)
0 Likes
Message 12 of 14

k005
Advisor
Advisor

 

 

How can I get L=xxx lengths with the same diameter(ø) values to be sorted here?

 

so how should I change the orderBy?

 

the desired listbox content:

 

5 120ø8 L = 181
6 25ø10 L = 140
9 15ø10 L = 140
1 25ø16 / 15 L = 300
4 80ø16 / 15 L = 429
2 30ø16 L = 620
3 14ø20 L = 330

8 25ø20 L = 398

7 25ø20 L = 561

0 Likes
Message 13 of 14

parikhnidi
Advocate
Advocate

Instead of using listbox , try using DataGridView control. If your original data is parsed (e.g. first row of the data has entries 5, 120, 8, 181), I assume most likely it is, you can easily populate these entries into the DataGridView and can be programmatically sorted in the order of your choice.

 

I have just started learning Forms in C#. I will be able to provide you with the solution at a future date in line with my assumption above.

 

Have fun,

Nimish

Message 14 of 14

k005
Advisor
Advisor

 

With Datagridview, of course. But the path I will follow is through Listbox. The reason is by design of the program I am making.

 

I think I just started learning. The way I learn is usually with code examples. But there are those who misinterpret this situation... No problem. I will continue. 🙂

 

Thanks.

0 Likes