|
-
Apr 16th, 2002, 12:54 PM
#1
Thread Starter
Addicted Member
ALphabetic Sort
How can I get this list of names to sort alphebetically? I know theres a loop in their that probably has nothing to do with it so tell if I have to delete or something. but how do I get it to sort alphabetically
Me.Cls
Dim number(1 To 20) As Integer
Dim counter, temp As Integer
Dim temp2 As String
Dim nme(1 To 20) As String
Dim height(1 To 20) As Integer
Dim weight(1 To 20) As Integer
number(1) = 2
number(2) = 4
number(3) = 6
number(4) = 12
number(5) = 9
number(6) = 16
number(7) = 22
number(8) = 34
number(9) = 21
number(10) = 14
number(11) = 27
number(12) = 18
number(13) = 33
number(14) = 89
number(15) = 43
number(16) = 32
number(17) = 29
number(18) = 31
number(19) = 19
number(20) = 20
nme(1) = "Vincent VanGogh"
nme(2) = "Wilfrid Laurier"
nme(3) = "Abraham Lincoln"
nme(4) = "Ludwig Beethoven"
nme(5) = "George Byron "
nme(6) = "George Washington"
nme(7) = "Winston Churchill"
nme(8) = "Albert Einstein"
nme(9) = "Leonardo DaVinci"
nme(10) = "Issac Newton"
nme(11) = "Galile Galileo"
nme(12) = "Stephen Hawking"
nme(13) = "Edgar Allan Poe"
nme(14) = "Carl Gauss"
nme(15) = "William Shakespeare"
nme(16) = "Maria Curie"
nme(17) = "John Nash"
nme(18) = "James Michener"
nme(19) = "Mahatma Gandhi"
nme(20) = "Genghis Khan"
height(1) = "150"
height(2) = "57 "
height(3) = "66"
height(4) = "59"
height(5) = "53"
height(6) = "52"
height(7) = "55"
height(8) = "61"
height(9) = "52"
height(10) = "54"
height(11) = "63"
height(12) = "52"
height(13) = "55 "
height(14) = "52"
height(15) = "57"
height(16) = "58"
height(17) = "62"
height(18) = "54"
height(19) = "59"
height(20) = "58"
weight(1) = "150"
weight(2) = "57 "
weight(3) = "66"
weight(4) = "59"
weight(5) = "53"
weight(6) = "52"
weight(7) = "55"
weight(8) = "61"
weight(9) = "52"
weight(10) = "54"
weight(11) = "63"
weight(12) = "52"
weight(13) = "55"
weight(14) = "52"
weight(15) = "57"
weight(16) = "58"
weight(17) = "62"
weight(18) = "54"
weight(19) = "59"
weight(20) = "58"
For Index = 1 To 18
For counter = 1 To 19
If weight(counter) > weight(counter + 1) Then
temp = weight(counter)
weight(counter) = weight(counter + 1)
weight(counter) = weight(counter + 1)
weight(counter + 1) = temp
End If
Next counter
Next Index
Print "This is the sorted list"
Print " Age"; " Height"; " Weight"
Print nme(1); number(1); height(1); weight(1)
Print nme(2); number(2); height(2); weight(2)
Print nme(3); number(3); height(3); weight(3)
Print nme(4); number(4); height(4); weight(4)
Print nme(5); number(5); height(5); weight(5)
Print nme(6); number(6); height(6); weight(6)
Print nme(7); number(7); height(7); weight(7)
Print nme(8); number(8); height(8); weight(8)
Print nme(9); number(9); height(9); weight(9)
Print nme(10); number(10); height(10); weight(10)
Print nme(11); number(11); height(11); weight(11)
Print nme(12); number(12); height(12); weight(12)
Print nme(13); number(13); height(13); weight(13)
Print nme(14); number(14); height(14); weight(14)
Print nme(15); number(15); height(15); weight(15)
Print nme(16); number(16); height(16); weight(16)
Print nme(17); number(17); height(17); weight(17)
Print nme(18); number(18); height(18); weight(18)
Print nme(19); number(19); height(19); weight(19)
Print nme(20); number(20); height(20); weight(20)
Print
End Sub
-
Apr 16th, 2002, 01:09 PM
#2
Hyperactive Member
Is this for an assignment or something? Are you allowed to use controls. A ListBox could sort them for you.
At least change that last bit:
VB Code:
Print "This is the sorted list"
Print " Age"; " Height"; " Weight"
For Counter = 1 To 20
Print nme(Counter); Number(Counter); Height(Counter); Weight(Counter)
Next
"Today's mighty oak is just yesterday's nut,
that held its ground."
-
Apr 16th, 2002, 01:41 PM
#3
Not NoteMe
If you used a list, then you'd have to run a bit of code that switched all the other things in the arrays you didn't put in the list, so that they were with the correct name.
Personally i would use a custom type to store the details, instead of all the arrays.
VB Code:
Private Type typPerson
Name as String
Age as Integer
Weight as Double
Height as Double
End Type
Dim People(1 To 20) as typPerson
People(1).Age = 2
People(1).Name = "Vincent VanGogh"
People(1).Height = 150
People(1).Weight = 150
etc....
Then to sort them...
VB Code:
Dim Temp As typPerson
For Index = 1 To 18
For counter = 1 To 19
If Person(counter).weight > Person(counter + 1).weight) Then 'sort by weight
temp = Person(counter)
Person(counter) = Person(counter + 1) 'I think this works
Person(counter) = Person(counter + 1)
Person(counter + 1) = temp
End If
Next counter
Next Index
This would correctly keep all their data with them (before it was just putting their weights into order, without moving them).
Quotes:
"I am getting better then you guys.." NoteMe, on his leet english skills.
"And I am going to meat her again later on tonight." NoteMe
"I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
"my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
Have I helped you? Please Rate my posts. 
-
Apr 16th, 2002, 01:56 PM
#4
Hyperactive Member
This might apply to one of your other posts.
Don't start multiple threads for the same question if possible 
Here is the 'sort by number' code:
(this is sorting by your number(1 to 20) array so it's not going to be alphabetical. But it should show you how to do your bubble sort)
You didn't have temps to cover all the items for a start.
Then you were assigning your temps and putting them back in the loop which will cause big probs. You get your temps out first, then do the "shift" loop. Then put your temps back after.
VB Code:
For index = 1 To 19
If number(index) > number(index + 1) Then
'Get the first values out and store them before the shift loop
tmpNumber = number(1)
tmpName = nme(1)
tmpHeight = height(1)
tmpWeight = weight(1)
For counter = 1 To 19
number(counter) = number(counter + 1)
nme(counter) = nme(counter + 1)
height(counter) = height(counter + 1)
weight(counter) = weight(counter + 1)
Next counter
'Now put those stored values back at the end
number(20) = tmpNumber
nme(20) = tmpName
height(20) = tmpHeight
weight(20) = tmpHeight
End If
Next index
Print "This is the sorted list"
Print " Age"; " Height"; " Weight"
For counter = 1 To 20
Print nme(counter); number(counter); height(counter); weight(counter)
Next
I haven't checked it out properly. It's 7am and I've been up all night so I could have got something wrong.
Hey, let us know ok
Last edited by Alan777; Apr 16th, 2002 at 02:05 PM.
"Today's mighty oak is just yesterday's nut,
that held its ground."
-
Apr 16th, 2002, 02:10 PM
#5
Not NoteMe
Incase you decide to use types, here is how you would do the sort using a bubble sort.
VB Code:
Dim Person as typPerson
Dim Temp as typPerson
Dim Flag as Boolean
Dim Count as Integer
Do While flag = False And n = 1
flag = False
For Count = 1 To 19
If Person(Count).Name > Person(Count + 1).Name Then
Temp = Person(Count)
Person(Count) = Person(Count + 1)
Person(Count + 1) = Temp
flag = True
End If
Next Count
n = n - 1
Loop
Quotes:
"I am getting better then you guys.." NoteMe, on his leet english skills.
"And I am going to meat her again later on tonight." NoteMe
"I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
"my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
Have I helped you? Please Rate my posts. 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|