?
Solved

Sort in ListBox

Posted on 2003-03-14
5
Medium Priority
?
390 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month9 days, 9 hours left to enroll

762 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