Sort a dynamic array of structs

Thread Solved

Join Date: Jan 2005
Posts: 15
Reputation: evil_dude_01 is an unknown quantity at this point 
Solved Threads: 0
evil_dude_01 evil_dude_01 is offline Offline
Newbie Poster

Sort a dynamic array of structs

 
0
  #1
Feb 16th, 2005
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.
Quick reply to this message  
Join Date: Feb 2005
Posts: 4
Reputation: dimuthubas is an unknown quantity at this point 
Solved Threads: 1
dimuthubas dimuthubas is offline Offline
Newbie Poster

Re: Sort a dynamic array of structs

 
0
  #2
Feb 17th, 2005
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 reply to this message  
Join Date: Jan 2005
Posts: 15
Reputation: evil_dude_01 is an unknown quantity at this point 
Solved Threads: 0
evil_dude_01 evil_dude_01 is offline Offline
Newbie Poster

Re: Sort a dynamic array of structs

 
0
  #3
Feb 17th, 2005
Hi, Thanks. I'm at college just now so i'll read it later.
Quick reply to this message  
Join Date: Jan 2005
Posts: 15
Reputation: evil_dude_01 is an unknown quantity at this point 
Solved Threads: 0
evil_dude_01 evil_dude_01 is offline Offline
Newbie Poster

Re: Sort a dynamic array of structs

 
0
  #4
Feb 18th, 2005
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. :-|
Quick reply to this message  
Join Date: Jan 2005
Posts: 15
Reputation: evil_dude_01 is an unknown quantity at this point 
Solved Threads: 0
evil_dude_01 evil_dude_01 is offline Offline
Newbie Poster

Re: Sort a dynamic array of structs

 
0
  #5
Feb 18th, 2005
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, 38 views)
Quick reply to this message  
Join Date: Jan 2005
Posts: 15
Reputation: evil_dude_01 is an unknown quantity at this point 
Solved Threads: 0
evil_dude_01 evil_dude_01 is offline Offline
Newbie Poster

Re: Sort a dynamic array of structs

 
0
  #6
Feb 19th, 2005
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, 21 views)
File Type: zip Array Nonsense.zip (11.2 KB, 18 views)
Quick reply to this message  
Join Date: Jan 2005
Posts: 15
Reputation: evil_dude_01 is an unknown quantity at this point 
Solved Threads: 0
evil_dude_01 evil_dude_01 is offline Offline
Newbie Poster

Re: Sort a dynamic array of structs

 
0
  #7
Feb 20th, 2005
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
Quick reply to this message  
Join Date: Jan 2005
Posts: 15
Reputation: evil_dude_01 is an unknown quantity at this point 
Solved Threads: 0
evil_dude_01 evil_dude_01 is offline Offline
Newbie Poster

Me Again :P

 
0
  #8
Feb 21st, 2005
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, 63 views)
Quick reply to this message  
Join Date: Jun 2006
Posts: 1
Reputation: strick9 is an unknown quantity at this point 
Solved Threads: 0
strick9 strick9 is offline Offline
Newbie Poster

Re: Sort a dynamic array of structs

 
0
  #9
Jun 16th, 2006
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
Quick reply to this message  
Closed Thread

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Visual Basic 4 / 5 / 6 Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC