Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Making a true arrainged list.

Status
Not open for further replies.

BadCook

Technical User
Sep 7, 2009
71
US
I want a sorted list contaning year and month entries such as "2013 1", "2013 11", "2013 12", "2013 2". The entries shown will arrainge as shown but is there a trick way to show them in true order or do I have to program them to get them time ordered?
Thanks for any help.
 
Try padding out the month data so it has equal lengths as in "2013 01", "2013 11", "2013 12", "2013 02" it should then sort out alphabetically.
 
> sort out alphabetically ...
sort out alphabetically into time order
 
“in true order”: January, November, December, February
Whatever happened to October?

Anyway, place a listbox (List1) on a Form, set its Sorted property to True.

Code:
Dim i As Integer

For i = 1 To 12
    List1.AddItem "2013 " & i
Next i

You get:
[tt]
2013 1
2013 10
2013 11
2013 12
2013 2
2013 3
2013 4
2013 5
2013 6
2013 7
2013 8
2013 9
[/tt]

Have fun.

---- Andy
 
As Hugh suggested, pad the months with zeros:

Place a listbox (List1) on a Form, set its Sorted property to True.

Code:
Private Sub Form_Load()
Dim i As Integer

For i = 1 To 12
    List1.AddItem "2013 " & Format(i, "00")
Next i
End Sub

You get:

2013 01
2013 02
2013 03
2013 04
2013 05
2013 06
2013 07
2013 08
2013 09
2013 10
2013 11
2013 12


[gray]Experience is something you don't get until just after you need it.[/gray]
 
Error7, that is not what OP wants

"entries such as "2013 1", "2013 11", "2013 12", "2013 2". "

so it should be:
[tt]
2013 1
2013 11
2013 12
2013 2
[/tt]
I still don't know what happened to [tt]
2013 10[/tt]


Have fun.

---- Andy
 
As an alternative to padding the data, have a look at thread222-1695412, where we discuss various ways of  sorting a list (naturally I am biased towards my own code there, which allows a custom sort to be applied to many of the .net collections/hashes/lists/arrays that be used in VB6

Or thread222-578008, where I provide a custom sort solution for the list view control
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top