944,181 Members | Top Members by Rank

Ad:
Feb 16th, 2005
0

Sort a dynamic array of structs

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
evil_dude_01 is offline Offline
15 posts
since Jan 2005
Feb 17th, 2005
0

Re: Sort a dynamic array of structs

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
Reputation Points: 10
Solved Threads: 1
Newbie Poster
dimuthubas is offline Offline
4 posts
since Feb 2005
Feb 17th, 2005
0

Re: Sort a dynamic array of structs

Hi, Thanks. I'm at college just now so i'll read it later.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
evil_dude_01 is offline Offline
15 posts
since Jan 2005
Feb 18th, 2005
0

Re: Sort a dynamic array of structs

Nope , Like i said i'm a begginer programmer and thats too complicated for me.

I can't believe there are no tutorials on the internet. I've looked at hundreds of web pages about sorting.

It must be easy to sort a few lines of data in a text file. :-|
Reputation Points: 10
Solved Threads: 0
Newbie Poster
evil_dude_01 is offline Offline
15 posts
since Jan 2005
Feb 18th, 2005
0

Re: Sort a dynamic array of structs

I have made a tiny program and uploaded it for you (anyone) to see. Sorry to be a pain.
Attached Files
File Type: zip Array Nonsense.zip (10.0 KB, 91 views)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
evil_dude_01 is offline Offline
15 posts
since Jan 2005
Feb 19th, 2005
0

Re: Sort a dynamic array of structs

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.
Attached Files
File Type: zip Sport.zip (12.8 KB, 51 views)
File Type: zip Array Nonsense.zip (11.2 KB, 42 views)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
evil_dude_01 is offline Offline
15 posts
since Jan 2005
Feb 20th, 2005
0

Re: Sort a dynamic array of structs

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
evil_dude_01 is offline Offline
15 posts
since Jan 2005
Feb 21st, 2005
0

Me Again :P

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.

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1.  
  2. Call BubbleSort(CustomerArray) 'To call the procedure
  3.  
  4. Private Sub BubbleSort(theArray() As CustomerRecord)
  5. Dim pass As Integer, compare As Integer
  6. Dim hold As CustomerRecord
  7. Dim CurrentNumber As Integer, NextNumber As Integer
  8. 'Customer Number is converted to an integer instead of a string
  9. 'otherwise when sorting it only converts the first character
  10.  
  11. For pass = 1 To (UBound(theArray) - 1)
  12. For compare = 1 To (UBound(theArray) - 1)
  13. CurrentNumber = theArray(compare).Number
  14. NextNumber = theArray(compare + 1).Number
  15. If CurrentNumber > NextNumber Then
  16. hold = theArray(compare)
  17. theArray(compare) = theArray(compare + 1)
  18. theArray(compare + 1) = hold
  19. End If
  20. Next compare
  21. Next pass
  22. 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.
Attached Files
File Type: zip Dynamic Array of Structs (Bubble Sort).zip (4.7 KB, 109 views)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
evil_dude_01 is offline Offline
15 posts
since Jan 2005
Jun 16th, 2006
0

Re: Sort a dynamic array of structs

Nice program Richard Bishop
i m a biginner and it really helped me a lot
thank you for creating and sharing it.
i hope that u have got a good note for it in ur college
good bye


ps:excuse my english i m french
Reputation Points: 10
Solved Threads: 0
Newbie Poster
strick9 is offline Offline
1 posts
since Jun 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in Visual Basic 4 / 5 / 6 Forum Timeline: Boolean checking
Next Thread in Visual Basic 4 / 5 / 6 Forum Timeline: audio level detection





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC