1

So my list box is populate with files from a folder. I want the files to be on a ascending way, but some files got the same name, and when u name something with the same name on windows it goes like, filename, filename (2), filename (3)... filename(10)...

But on the listbox it goes like filename (10), filename (11), filename (2), filename (3)... filename

And I want it to be exactly how windows go...

I tried this but no results

        items = ListBox1.Items.OfType(Of Object)().ToList()
    ListBox5.Items.AddRange(items.OrderBy(Function(i) i).ToArray())
Lftbrito
  • 63
  • 5
  • [Natural Sort Order in C#](https://stackoverflow.com/q/248603/7444103) (the same applies to VB.Net). The accepted answer PInvokes `StrCmpLogicalW`: it's a choice, actually used relatively often. You can also use the extension method show in [another answer](https://stackoverflow.com/a/11720793/7444103): the extension is applied to `IEnumerable` collections (so, List, Array etc.). If you're interested, I suggest to change the return type to `IEnumerable` instead of `IOrderedEnumerable` (changing `return source.OrderBy(...) as IEnumerable`) and possibly `yield return` each value. – Jimi Sep 28 '20 at 16:37
  • Well, there are three answers that uses the same Win23 function in VB.Net, [here](https://stackoverflow.com/a/52860627/7444103), [here](https://stackoverflow.com/a/61648168/7444103) and [here](https://stackoverflow.com/a/35367605/7444103). All three can be turned into an extension method, using a `Func` as selector instead of a comparer. – Jimi Sep 28 '20 at 16:42

1 Answers1

0

Try this:

items = ListBox1.Items
System.Array.Sort(items)

Then iterate over items with a For Each loop.

Wasif
  • 14,755
  • 3
  • 14
  • 34