Link to home
Start Free TrialLog in
Avatar of tobin46
tobin46

asked on

Sort in ListBox

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.
Avatar of hazgod
hazgod

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
 listNames.additem names(index)
 listAverages.additem averages(index)
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
Avatar of tobin46

ASKER

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?

Read the file
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?
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...
ASKER CERTIFIED SOLUTION
Avatar of hazgod
hazgod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
tobin46:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
Experts: Post your closing recommendations!  Who deserves points here?