Solved

Sort in ListBox

Posted on 2003-03-14
Medium Priority
391 Views
How do I now add these items to the listbox in order of the average?  In other words sorted by average.

The file is 50 employees with numbers after their name:
ex: John 50,50,40,40,30

I am able to now calculate the average and place these items into a listbox in alphabetical order.

Also thanks to all who answered before I used the advice of all.  It is greatly appreciated by a rookie to VB.
0
Question by:tobin46

LVL 1

Expert Comment

ID: 8140092
try using 2 arrays.

dim names(80) as string
dim averages(80) as integer
dim thisName as string
dim thisAverage as string
dim num1 as integer
dim num2 as integer
dim num3 as integer
dim index as integer
dim count as integer

index = 0
open "scores.txt" for input as #1
do until EOF
input #1, thisName, num1, num2, num3 (etc)
names(count) = thisName
thisAverage = (num1 + num2 + num3) / 3
averages(count) = thisAverage
count = count + 1
loop
close #1

you then have 2 list boxes (1 for names and 1 for averages).

for index = 0 to count
next

this code will put the names alone side the numbers. if you need to sort the list, add in 3 more vaiables (swapped as boolean and tempName as string and tempAverage as integer) then add this code before for index = 0 to count:

do while swapped = true
swapped = false
for index = 0 to count
if averages(index) > average(index+1)
tempAverage = average(index)
average(index) = average(index+1)
average(index+1) = tempAverage
tempName = names(index)
names(index) = names(index+1)
names(index+1) = tempName
swapped = true
end if
next
loop

hope this helps
0

LVL 1

Author Comment

ID: 8141747
How can I be sure that the average will correspond with the name across from it in the opposite list box?  Wouldn't I have to calculate a MAX of the averages then AddItem according to average?

Calculate the averages
Sort the averages from Greatest to least
Write the records to a list box according to average from least to greatest.

How do I accomplish the last two steps?
0

Expert Comment

ID: 8142150
First open the textfile.
Calculate avarage and put it in front of the name (e.g. 50 John)
Put it in a second not visible listbox
Sort that listbox (easy because the avarage is in front of it...)
Now copy the listbox values to the visible listbox using a do loop (and a Split() function)

Sorry I not provide any code...
0

LVL 1

Accepted Solution

hazgod earned 80 total points
ID: 8142157
do while swapped = true
swapped = false
for index = 0 to count
if averages(index) < average(index+1)
tempAverage = average(index)
average(index) = average(index+1)
average(index+1) = tempAverage
tempName = names(index)
names(index) = names(index+1)
names(index+1) = tempName
swapped = true
end if
next
loop

this is changed from greater than to less than. the loop will keep looping until they are sorted and none of the positions are swapped. the names are swapped at the same time as the averages so they will always be in line with each other
0

Expert Comment

ID: 8900063
tobin46:
Experts: Post your closing recommendations!  Who deserves points here?
0

Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month8 days, 3 hours left to enroll