Results 1 to 5 of 5

Thread: ALphabetic Sort

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2002
    Posts
    225

    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

  2. #2
    Hyperactive Member Alan777's Avatar
    Join Date
    Jan 2001
    Location
    New Zealand
    Posts
    303
    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:
    1. Print "This is the sorted list"
    2. Print " Age"; " Height"; " Weight"
    3.  
    4. For Counter = 1 To 20
    5.  Print nme(Counter); Number(Counter); Height(Counter); Weight(Counter)
    6. Next
    "Today's mighty oak is just yesterday's nut,
    that held its ground."

  3. #3
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051
    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:
    1. Private Type typPerson
    2.     Name as String
    3.     Age as Integer
    4.     Weight as Double
    5.     Height as Double
    6. End Type
    7. Dim People(1 To 20) as typPerson
    8.  
    9. People(1).Age = 2
    10. People(1).Name = "Vincent VanGogh"
    11. People(1).Height = 150
    12. People(1).Weight = 150
    etc....

    Then to sort them...
    VB Code:
    1. Dim Temp As typPerson
    2.  
    3. For Index = 1 To 18
    4.     For counter = 1 To 19
    5.         If Person(counter).weight > Person(counter + 1).weight) Then 'sort by weight
    6.             temp = Person(counter)
    7.             Person(counter) = Person(counter + 1) 'I think this works
    8.             Person(counter) = Person(counter + 1)
    9.             Person(counter + 1) = temp
    10.         End If
    11.     Next counter
    12. 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.


  4. #4
    Hyperactive Member Alan777's Avatar
    Join Date
    Jan 2001
    Location
    New Zealand
    Posts
    303
    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:
    1. For index = 1 To 19
    2.       If number(index) > number(index + 1) Then
    3.            'Get the first values out and store them before the shift loop
    4.            tmpNumber = number(1)
    5.            tmpName = nme(1)
    6.            tmpHeight = height(1)
    7.            tmpWeight = weight(1)
    8.  
    9.            For counter = 1 To 19
    10.               number(counter) = number(counter + 1)
    11.               nme(counter) = nme(counter + 1)
    12.               height(counter) = height(counter + 1)
    13.               weight(counter) = weight(counter + 1)
    14.            Next counter
    15.  
    16.            'Now put those stored values back at the end
    17.            number(20) = tmpNumber
    18.            nme(20) = tmpName
    19.            height(20) = tmpHeight
    20.            weight(20) = tmpHeight
    21.       End If
    22. Next index
    23.  
    24. Print "This is the sorted list"
    25. Print " Age"; " Height"; " Weight"
    26.  
    27. For counter = 1 To 20
    28.    Print nme(counter); number(counter); height(counter); weight(counter)
    29. 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."

  5. #5
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051
    Incase you decide to use types, here is how you would do the sort using a bubble sort.

    VB Code:
    1. Dim Person as typPerson
    2. Dim Temp as typPerson
    3. Dim Flag as Boolean
    4. Dim Count as Integer
    5. Do While flag = False And n = 1
    6.     flag = False
    7.     For Count = 1 To 19
    8.         If Person(Count).Name > Person(Count + 1).Name Then
    9.             Temp = Person(Count)
    10.             Person(Count) = Person(Count + 1)
    11.             Person(Count + 1) = Temp
    12.             flag = True
    13.         End If
    14.     Next Count
    15.     n = n - 1
    16. 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
  •  



Click Here to Expand Forum to Full Width