Sign up to the jQuery Grid Subscription list.

List Box Resort Solution

Original Post by RobertMc at http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2454479&SiteID=1

The Question :

I have a listbox with roughly 2500 items that were sorted when created. Now I want to re-sort the same listbox using the time and date portion of the string. Shown below are 5 items as is and then how I'd like the re-sort to place them in the listbox.

Initial Sort

25536_002 9/20/2007 10:37:04 PM
25536_002 9/20/2007 10:41:04 PM
25536_002 8/27/2007 8:41:17 AM
26875_001 9/20/2007 1:48:18 PM
26875_001 9/19/2007 12:38:12 PM

Final Sort

25536_002 8/27/2007 8:41:17 AM
25536_002 9/20/2007 10:37:04 PM
25536_002 9/20/2007 10:41:04 PM
26875_001 9/19/2007 12:38:12 PM
26875_001 9/20/2007 1:48:18 PM

I am hoping for a simple resolution.

Thanks,
RobertMC

The Accepted Answer (by Solitaire)


This is not so simple, involving some string manipulation, but I tested it with your sample data and it works.

Code Block
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim L As Integer, strdat As String
L = ListBox1.Items.Count
Dim strdate(L) As String
For x As Integer = 0 To L - 1
ListBox1.SelectedIndex = x
strdat = ListBox1.SelectedItem.ToString.Substring(20)
strdate(x) = strdat.PadRight(13) & (ListBox1.SelectedItem.ToString).Substring(0, 20)
Next x
ListBox1.Items.Clear()
For x As Integer = 0 To L - 1
ListBox1.Items.Add(strdate(x))
Next x
ListBox1.Sorted = True
For x As Integer = 0 To L - 1
ListBox1.SelectedIndex = x
strdat = ListBox1.SelectedItem.ToString.Substring(14)
strdate(x) = strdat & (ListBox1.SelectedItem.ToString).Substring(0, 13)
Next x
ListBox1.Items.Clear()
For x As Integer = 0 To L - 1
ListBox1.Items.Add(strdate(x))
Next x
End Sub

0 comments:

Related Ads