If this is your first visit, be sure to
check out the
FAQ
by clicking the
link above. You may have to
register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.
Hi there, I have a listbox called pointslist, it has about 31 numbers, what I would like to do is arrange them from highest to lowest. Would anyone know of a way to reorder them?
Thank you!!
List boxes do not do numeric sorting so there is an issue expecting it to sort numbers at all as numbers with different numbers of digits will show up out of the proper sort order. i.e. 10 before 2 for example 20 before 3
If you want the items sorted in reverse order then the only way I know of is to remove the items from the list, sort them and put them back in the order you want them to appear making sure that you have sort turned off on the listbox else it will sort them the way it likes.
You can do a reverse sort with a listview but be aware that is also an alpha sort so it will run into the same issues with numeric data that is not the same length
I do not know what you are trying to do but if you want then numbers sorted then you will have to use a sort routine of some sort. Hard to say what would be best without knowing where the data is coming from and what format it is in.
To answer your question though yes you can get the val of the caption in a label array but that does nothing to sort data and is nothing like a list box so I would not think that would be an option. Like I said before a listview can do a reverse sort. It also has some of the abilities of a list box such as scrolling and the ability to add or remove items on the fly.
In general it is better to state what you are trying to accomplish rather than how you are trying to do it as often there are better ways than you may think.
Good idea, I'll give more info.
What I would like to do is arrange from highest to lowest, 31 numbers. These numbers are random, anywhere between 0-100. The numbers are already generated, but I would like to arrange them from the highest number at the top (or perhaps in the first label caption array), and then the next biggest in array index 1, and etc.
Here is my first attempt, simple code.
It checks if its bigger (or smaller) then the entry after and sorts it one step at a time... but I made it loop so it sorts all of it.
Code:
Private Sub SortListBox(LB As ListBox, Optional HighestToLowest As Boolean = False)
Dim LngArr() As Long
Dim Lng As Long
Dim i As Integer
Dim QuitSort As Boolean
ReDim LngArr(LB.ListCount - 1)
Do While QuitSort = False
QuitSort = True
For i = 0 To LB.ListCount - 1
LngArr(i) = LB.List(i)
For i = 0 To LB.ListCount - 1
If Not i = LB.ListCount - 1 Then
If HighestToLowest = False Then
If LngArr(i) > LngArr(i + 1) Then
Lng = LngArr(i)
LngArr(i) = LngArr(i + 1)
LngArr(i + 1) = Lng
QuitSort = False
End If
If LngArr(i) < LngArr(i + 1) Then
Lng = LngArr(i)
LngArr(i) = LngArr(i + 1)
LngArr(i + 1) = Lng
QuitSort = False
End If
End If
End If
For i = 0 To LB.ListCount - 1
LB.List(i) = LngArr(i)
DoEvents
End Sub
Private Sub Command1_Click()
SortListBox List1, True
'SortListBox List1 'For Lowest To Highest
End Sub
Code could be shortened.
Use
two
listboxes.
A sorted one that's
not
visible. Load values into this one, sorting them in the process.
An unsorted one that's
visible
. Transfer values to here from the sorted list - but last item first.
Code:
lbVisible.Clear
For iSub = lbSorted.ListCount -1 to 0 Step -1
lbVisible.AddItem lbSorted.List( iSub )
Regards, Phill W.
I don't think it will sort properly with a sorted listbox as datamiser pointed out. Post #2
If you sort using < or > then you can't go wrong.
JustinM, I think you should create 2 list boxes on your form. the first 1, say lstAsc. change the sorted property to true.
on the second list box, say lstDesc, run this code
sub sortLstDesc()
on error resume next
dim i as integer
for i=lstAsc.Listcount-1 to 0
lstDesc.AddItem lstAsc.List(i)
next i
End Sub
Please be sure lstAsc.Sorted=True and lstDesc.Sorted=False. Copy this code and paste and run...
Option Explicit
Dim i As Integer
Sub sortLstDesc()
On Error Resume Next
For i = 0 To lstAsc.ListCount - 1
lstDesc.AddItem lstAsc.List(lstAsc.ListCount - i)
Next i
End Sub
Private Sub Form_Load()
sortLstDesc
End Sub
Private Sub Form_Load()
Dim i As Long
Const MaxLen As Long = 2 'increase for longer expected numbers
'sorted listbox
Randomize
For i = 1 To 20
List1.AddItem Format$(Int(Rnd * 50), String$(MaxLen, "@")) 'right justify the number
End Sub
The two list box idea will work provided you pad the numbers in the sorted list with leading 0s up to the max length. In other words your list entries would be 001 002 ---- 010 --- 100 This will allow the numbers to sort int he proper order from 1 to 100 then you can use the reverse loop to read the items from the sorted list, convert them to a number which will drop the leading 0s and add them to the non sorted list in reverse order
Thank you guys for your help! These are great solutions!
I was wondering about an addition. Each point belongs to a student. So for ex: Bob might have 5 points, Sue might have 7, Jim might have 4. Thanks for your guy's help, we can sort out the numbers in order from high - low, but lets say I wanted to put the student name beside their point. Perhaps the format could be Bob = 5?
So far the list will order the numbers, but I am puzzled as how I can link the number to the student. Right now, The student points are in a label caption array called PointCountLBL, and the array with the student names is called StudentLBL. So StudentLBL(0), and PointCountLBL(0) would refer to one student. There are 31 labels in each array, would there be a way to order the numbers and keep the student's name with it? Or would it be easier to just rearrange the label captions? I think the list works awesome, but it wouldn't be able to keep the student name with it?
Perhaps I could make a second list. The second list could have all of the student names in it. Could we reorder the second list the order as the list1? For ex: if the top number is moved down to index 5 or something, could the top list in list2 drop down to the same spot?
Thanks for all your help on this!!
I was wondering about an addition. Each point belongs to a student. So for ex: Bob might have 5 points, Sue might have 7, Jim might have 4. Thanks for your guy's help, we can sort out the numbers in order from high - low, but lets say I wanted to put the student name beside their point. Perhaps the format could be Bob = 5?
So far the list will order the numbers, but I am puzzled as how I can link the number to the student.
What you're talking about now, is called "a record" (things that belong to a given "entity" - in your case "a Student").
And such things are usually handled in a DataBase(Table), which is only a few lines of code away from you.
If you want to avoid a DataBase in your App at this point in time (although I see no reason to wait any longer with that),
you can make use of a pretty nice container-object, which comes "for free" (preinstalled without any setup) on
any Win-OS >= W2K.
This container is the ADODB.Recordset, which in such smaller scenarios as yours is usable also LateBound
(without checking in any external References into your project).
Just look at an ADO-Recordset the same way as you look at "a VB.Collection with multiple Columns" or something like that.
As said, this List-Container-Object is quite powerful - since it allows DB-independent Sorting and Filtering -
just stuff your original Data into it, as the "one place to keep your Students-Data - or better: Student-Records" -
and treat the VB.ListBox only as its "visual reflection".
Below is some Code, which shows how to make use of this Object:
Into a Form (with a List1-Control on it) - then Click on the Form:
Code:
Option Explicit
Private Students As Object
Private Sub Form_Load()
Set Students = CreateObject("ADODB.Recordset")
Students.Fields.Append "Name", 202, 4000 '202=adVarWChar
Students.Fields.Append "Points", 3 '3=adInteger
Students.Open
FillInExampleData
End Sub
Private Sub Form_Click()
FillList
MsgBox "Un-Ordered, as added to the Recordset"
FillList "Points Asc"
MsgBox "Ordered by Points, ascending"
FillList "Points Desc"
MsgBox "Ordered by Points, descending"
FillList "Points Desc, Name Asc"
MsgBox "Ordered by Points Desc and Name Asc (look at Alice and Aaron which have the same Points"
FillList "Name Asc"
MsgBox "Ordered by Name, ascending"
FillList
MsgBox "and un-ordered again"
End Sub
Private Sub FillList(Optional ByVal OrderByClause As String)
Dim i As Long
Students.Sort = OrderByClause
List1.Clear
For i = 1 To Students.RecordCount
Students.AbsolutePosition = i
List1.AddItem Students!Points & vbTab & Students!Name
Next i
End Sub
Private Sub FillInExampleData()
Students.AddNew Array("Name", "Points"), Array("Bob", 12)
Students.AddNew Array("Name", "Points"), Array("Sue", 22)
Students.AddNew Array("Name", "Points"), Array("Jim", 19)
Students.AddNew Array("Name", "Points"), Array("Aaron", 7)
Students.AddNew Array("Name", "Points"), Array("Alice", 7)
Students.AddNew Array("Name", "Points"), Array("Peter", 1)
Students.AddNew Array("Name", "Points"), Array("Paul", 15)
Students.AddNew Array("Name", "Points"), Array("Mary", 31)
End Sub
Advertiser Disclosure:
Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.