| | |
Sort a dynamic array of structs
Thread Solved |
•
•
Join Date: Jan 2005
Posts: 15
Reputation:
Solved Threads: 0
Please help me !!!!!
I want to know how to sort a list of records in a text file.
I want to sort them by customer number.
Here's whats in my module
Public CustomerArray() As CustomerRecord
Public Type CustomerRecord
Number As String
Name As String
Address As String
Town As String
PostCode As String
Phone As String
Email As String
PromoInfo As String
End Type
I can add new records, update records, delete records and display records.
I want to be able to display them all. I.E. load the first record into my form thenwhen i press next it will display the next number.
I have searched through loads of websites and i can find plenty of search algorithms but i'm new at this so i dont know how to get my records into them.
I dont want to use bubble sort because every website says its slow.
If you want i'll email you the code for my program.
I want to know how to sort a list of records in a text file.
I want to sort them by customer number.
Here's whats in my module
Public CustomerArray() As CustomerRecord
Public Type CustomerRecord
Number As String
Name As String
Address As String
Town As String
PostCode As String
Phone As String
Email As String
PromoInfo As String
End Type
I can add new records, update records, delete records and display records.
I want to be able to display them all. I.E. load the first record into my form thenwhen i press next it will display the next number.
I have searched through loads of websites and i can find plenty of search algorithms but i'm new at this so i dont know how to get my records into them.
I dont want to use bubble sort because every website says its slow.
If you want i'll email you the code for my program.
•
•
Join Date: Feb 2005
Posts: 4
Reputation:
Solved Threads: 1
Hi ..
Quick sort is the best way than Bubble Sort..
Check this out ..
This is something I found in the internet.. hope this might help u to get it done. Since I’m busy with a new development I’m not willing to modify u’r code at this time..I’m very sorry
Just go through this sample code.. Defiantly it might fulfill u’r goal.
This code is working and hopefully u might able to get it done..
http://www.vb-helper.com/howto_sorted_dir.html
http://www.hobbithouseinc.com/VB/quicksort%20sub.txt
Quick sort is the best way than Bubble Sort..
Check this out ..
This is something I found in the internet.. hope this might help u to get it done. Since I’m busy with a new development I’m not willing to modify u’r code at this time..I’m very sorry
Just go through this sample code.. Defiantly it might fulfill u’r goal.
This code is working and hopefully u might able to get it done..
http://www.vb-helper.com/howto_sorted_dir.html
http://www.hobbithouseinc.com/VB/quicksort%20sub.txt
•
•
Join Date: Jan 2005
Posts: 15
Reputation:
Solved Threads: 0
Right, this is my last plea for help. I've made another example of what i want sorted. This time i've included a few customer records and displayed them out in a list box. I've deliberately put the records randomly to show you why i need them in order.
I dont really care what sorting method i use as long as it displays the records by customer number.
Thanks.
Oh and the post i made before this one, the program contains sevral errors. I've fixed them but i can't edit the previous post.
I dont really care what sorting method i use as long as it displays the records by customer number.
Thanks.
Oh and the post i made before this one, the program contains sevral errors. I've fixed them but i can't edit the previous post.
•
•
Join Date: Jan 2005
Posts: 15
Reputation:
Solved Threads: 0
Solution.
I got my dad to help me, and he found a way to do it.
Here is the code.
Private Sub SortCustomers() 'Bubble Sort
Dim Pass As Integer
Dim Compare As Integer
Dim Hold As CustomerRecord
Dim Key, Key2 As Integer 'To convert the customer number from a string to an integer
For Pass = 1 To (UBound(CustomerArray) - 1)
For Compare = 1 To (UBound(CustomerArray) - 1)
Key = CustomerArray(Compare).Number
Key2 = CustomerArray(Compare + 1).Number
If Key > Key2 Then
Hold.Number = CustomerArray(Compare).Number
Hold.Name = CustomerArray(Compare).Name
Hold.CompanyName = CustomerArray(Compare).CompanyName
Hold.Address = CustomerArray(Compare).Address
Hold.Town = CustomerArray(Compare).Town
Hold.PostCode = CustomerArray(Compare).PostCode
Hold.Phone = CustomerArray(Compare).Phone
Hold.Email = CustomerArray(Compare).Email
Hold.PromoInfo = CustomerArray(Compare).PromoInfo
CustomerArray(Compare) = CustomerArray(Compare + 1)
CustomerArray(Compare + 1).Number = Hold.Number
CustomerArray(Compare + 1).Name = Hold.Name
CustomerArray(Compare + 1).CompanyName = Hold.CompanyName
CustomerArray(Compare + 1).Address = Hold.Address
CustomerArray(Compare + 1).Town = Hold.Town
CustomerArray(Compare + 1).PostCode = Hold.PostCode
CustomerArray(Compare + 1).Phone = Hold.Phone
CustomerArray(Compare + 1).Email = Hold.Email
CustomerArray(Compare + 1).PromoInfo = Hold.PromoInfo
End If
Next Compare
Next Pass
End Sub
To call the function type: Call SortCustomers
I got my dad to help me, and he found a way to do it.
Here is the code.
Private Sub SortCustomers() 'Bubble Sort
Dim Pass As Integer
Dim Compare As Integer
Dim Hold As CustomerRecord
Dim Key, Key2 As Integer 'To convert the customer number from a string to an integer
For Pass = 1 To (UBound(CustomerArray) - 1)
For Compare = 1 To (UBound(CustomerArray) - 1)
Key = CustomerArray(Compare).Number
Key2 = CustomerArray(Compare + 1).Number
If Key > Key2 Then
Hold.Number = CustomerArray(Compare).Number
Hold.Name = CustomerArray(Compare).Name
Hold.CompanyName = CustomerArray(Compare).CompanyName
Hold.Address = CustomerArray(Compare).Address
Hold.Town = CustomerArray(Compare).Town
Hold.PostCode = CustomerArray(Compare).PostCode
Hold.Phone = CustomerArray(Compare).Phone
Hold.Email = CustomerArray(Compare).Email
Hold.PromoInfo = CustomerArray(Compare).PromoInfo
CustomerArray(Compare) = CustomerArray(Compare + 1)
CustomerArray(Compare + 1).Number = Hold.Number
CustomerArray(Compare + 1).Name = Hold.Name
CustomerArray(Compare + 1).CompanyName = Hold.CompanyName
CustomerArray(Compare + 1).Address = Hold.Address
CustomerArray(Compare + 1).Town = Hold.Town
CustomerArray(Compare + 1).PostCode = Hold.PostCode
CustomerArray(Compare + 1).Phone = Hold.Phone
CustomerArray(Compare + 1).Email = Hold.Email
CustomerArray(Compare + 1).PromoInfo = Hold.PromoInfo
End If
Next Compare
Next Pass
End Sub
To call the function type: Call SortCustomers
•
•
Join Date: Jan 2005
Posts: 15
Reputation:
Solved Threads: 0
Yes i know i'm talking to myself but what the hell.
For anyone interested, the above method i showed is bad. Here is a better way.
Also i have attached a program that i'm quite please with. It allows you to add records to an array, Display the array, Save the array to file, Search for records in the array, Goto the next and previous records.
Not bad for a begginer.
For anyone interested, the above method i showed is bad. Here is a better way.
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
Call BubbleSort(CustomerArray) 'To call the procedure Private Sub BubbleSort(theArray() As CustomerRecord) Dim pass As Integer, compare As Integer Dim hold As CustomerRecord Dim CurrentNumber As Integer, NextNumber As Integer 'Customer Number is converted to an integer instead of a string 'otherwise when sorting it only converts the first character For pass = 1 To (UBound(theArray) - 1) For compare = 1 To (UBound(theArray) - 1) CurrentNumber = theArray(compare).Number NextNumber = theArray(compare + 1).Number If CurrentNumber > NextNumber Then hold = theArray(compare) theArray(compare) = theArray(compare + 1) theArray(compare + 1) = hold End If Next compare Next pass End Sub
Also i have attached a program that i'm quite please with. It allows you to add records to an array, Display the array, Save the array to file, Search for records in the array, Goto the next and previous records.
Not bad for a begginer.
![]() |
Similar Threads
Other Threads in the Visual Basic 4 / 5 / 6 Forum
- Previous Thread: Boolean checking
- Next Thread: audio level detection
| Thread Tools | Search this Thread |
* 6 429 2007 access activex add age application basic beginner birth bmp calculator cd cells.find click client code college component connection connectionproblemusingvb6usingoledb copy creat ctrl+f data database datareport date delete dissertations dissertationthesis dissertationtopic edit error excel excelmacro file filename form hardware header iamthwee image inboxinvb internetfiledownload keypress label listbox listview liveperson login looping machine microsoft movingranges number objectinsert open oracle password prime program prompt range-objects readfile reading record refresh remotesqlserverdatabase report save search sendbyte sites sort sql sql2008 sqlserver subroutine tags textbox time urldownloadtofile vb vb6 vb6.0 vba visual visualbasic visualbasic6 web window windows





