?
Solved

Sort in ListBox

Posted on 2003-03-14
5
Medium Priority
?
391 Views
Last Modified: 2012-05-04
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
Comment
Question by:tobin46
5 Comments
 
LVL 1

Expert Comment

by:hazgod
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
 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
0
 
LVL 1

Author Comment

by:tobin46
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?

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?
0
 

Expert Comment

by:Siebe
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

by:
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

by:CleanupPing
ID: 8900063
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?
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

616 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question