Hey Everyone, I'm stuck... I've got a 1-dimensional array from a text file that is displaying in a list box. I have buttons to sort ascending and descending but cannot seem to get them to work. My code for the array is

Dim name() As String = IO.File.ReadAllLines("Countries.txt")
lstCountries.Items.AddRange(name)

This code is successfully populating the listbox at launch. I've tried to pass this to the subs for the buttons when clicked are to sort and been unsuccessful so I have the original line in the subroutine as well. The code I've written to sort sub is:

            Dim name() As String = IO.File.ReadAllLines("Countries.txt")
            Dim sortdown = From var In name
                           Order By name Descending
            For Each var As String In sortdown
            Next
            lstCountries.Items.Clear()
            lstCountries.Items.AddRange(name)
End Sub

Its not working at all. It clears the listbox and repopulates the alphabetical list as originally placed in the box. I have made sure that the listbox sorting is turned off as well.
Any help would be greatly appreciated. I feel like I'm on the right track but missing something.

Dani AI

Generated

The observed behavior comes from two issues: the LINQ query in the button handler never replaces the listbox contents with the query result, and the query syntax uses the wrong identifier in the Order By clause. LINQ produces a new sequence — that new sequence must be materialized (ToArray/ToList) and added back to the ListBox. Also note 's suggestion: setting the ListBox's Sorted property will sort automatically, but it only enforces an ascending string sort and offers no control over case sensitivity or descending order.

A straightforward fix (LINQ method syntax) is to read the file, trim/ignore empty lines, order descending, clear the ListBox, then add the sorted array:

Dim countries = System.IO.File.ReadAllLines("Countries.txt")
Dim sortedDesc = countries.
                 Where(Function(s) Not String.IsNullOrWhiteSpace(s)).
                 Select(Function(s) s.Trim()).
                 OrderByDescending(Function(s) s).
                 ToArray()
lstCountries.Items.Clear()
lstCountries.Items.AddRange(sortedDesc)

An alternative using the CLR sort is slightly faster for large arrays and lets a comparer be specified:

Dim lines = System.IO.File.ReadAllLines("Countries.txt")
Array.Sort(lines, StringComparer.CurrentCultureIgnoreCase)  ' ascending
Array.Reverse(lines)                                       ' make descending
lstCountries.Items.Clear()
lstCountries.Items.AddRange(lines)

Troubleshooting notes: ensure ListBox.Sorted is False when manually reloading with a custom order (otherwise the control will re-sort the items), trim strings before comparing to avoid leading/trailing-space surprises, and use StringComparer for predictable culture/case behavior.

For (moving items between listboxes): remove items from the source while iterating the selected indices in reverse order so indices remain valid; add each removed item to the destination ListBox.

Recommended Answers

All 2 Replies

You want to sort items on listbox?
Try this :

lstCountries.Sorted = True

hi every one..i have encountered a system that needs a code for the list item on the list box to put to other listbox..can you help me how to do it??

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.