Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Finding a word's alphabetical position in a list

Status
Not open for further replies.

aliinal

Technical User
Jun 26, 2001
104
TR
Hi!

I'm writing an telehpone book program. And i use the format

nameandsurname;phonenumberone;phonenumbertwo;phonenumberthree

in the file. I seperate all the values while reading.
When i get the new entry, i want to write it to the alphabetical order place in the file. So when i read from file the list will be in alphabetical order.
How can i do it?
thank you
 

Hello...

Well, here's an example using SWAP to put names in alphabetical order.

'------------------------
'Example of name sorting
'------------------------

NumberOfEntries = 4 'how many names to sort

DIM Names(NumberOfEntries) AS STRING 'Make room for them

'Now let's make up some names to use, not in order of course

Names(1) = "Nathan"
Names(2) = "Amy"
Names(3) = "Zimmy"
Names(4) = "Daniel"

'Let's see the order before sorting...

PRINT "=== Before Sorting ==="
FOR t% = 1 TO NumberOfEntries
PRINT Names(t%)
NEXT

'Now let's put them in alphabetical order...

FOR y% = 1 TO NumberOfEntries
FOR x% = y% TO NumberOfEntries
IF Names(y%) > Names(x%) THEN
SWAP Names(y%), Names(x%)
END IF
NEXT
NEXT

'Ok, that should have done it.
'Let's see the names in Alphabetical order

PRINT "=== After sorting ==="
FOR t% = 1 TO NumberOfEntries
PRINT Names(t%)
NEXT

'-----------------------------
'END EXAMPLE
'-----------------------------

Hope this helps.

- Dav
 
Thanks Dav!

You've solved it!
You're not user so i can not vote for you!

But thank you very much!
 
There is also a FAQ on sort methods, which describes the quicksort (a very efficient sort method) quite well. If you're finding that 1,000 elements take a long time to sort with the above posted method (known as the 'bubble sort'), you may wish to look into the quicksort.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top