Alphabetical sort

BlitzPlus Forums/BlitzPlus Programming/Alphabetical sort

Paulo(Posted 2004) [#1]
Hi,
Im trying to write a small database program for fun, can anyone explain to me how to alphetically sort a list of names and surnames ?
Ive looked at an example in the code archives on how to do this, but its using types and Im using arrays, which have stored strings and it isnt clear to me how that example works. (below)

[url]http://www.blitzbasic.com/codearcs/codearcs.php?code=181[/url]

I've managed to sort them based on the lowercase ASCII value of the 1st letter of each name, as long as all 5 names start with a different letter it all works...

So next I have to consider how to calculate what name comes first in the list.
So how can I extend my first letter test to the whole name, please, or am i going about it all wrong? thanks in advance. Any help is much appreciated.


Gabriel(Posted 2004) [#2]
Blitz does this for you. You can just compare strings as though they were numbers.

Eg :

If "AAA"<"AAB" Then Print "AAA COMES BEFORE AAB ALPHABETICALLY"
If "AAC">"AAB" Then Print "AAC COMES AFTER AAB ALPHABETICALLY"



soja(Posted 2004) [#3]
I think you must be going about it all wrong, because it's very simple to sort strings alphabetically by using the < and > operators. <EDIT> Yeah, just like Sybixsus said

For a simple example, try this:
a$ = "Freddy"
b$ = "Frankie"
If a$ < b$ Notify a$+", "+b$ Else Notify b$+", "+a$

...and here's a very simple sorting algorithm (don't use it for a great number of cases because it's not that fast, but for a small number, it's very easy)
Dim names$(7)
names(1) = "FREddy"
names(2) = "Fred"
names(3) = "frankie"
names(4) = "Zachary"
names(5) = "Aaron"
names(6) = "frank"
names(7) = "FRANKY"

For i = 1 To 7
	Print "names("+i+") = " + names(i)
Next

sort(7)
Print "---"

For i = 1 To 7
	Print "names("+i+") = " + names(i)
Next
WaitKey

Function sort(depth)
	If depth = 1 Then Return
	low% = 1
	For i = 2 To depth
		If Upper(names(i)) > Upper(names(low)) Then low = i
	Next
	temp$ = names(depth)
	names(depth) = names(low)
	names(low) = temp
	sort(depth - 1)
End Function



Paulo(Posted 2004) [#4]
*drops head on desk with thud*

The penny drops.

Thanks guys. :)

edit, Soja, i just noticed it was the same example you gave me a while ago for sorting numbers! hehe


soja(Posted 2004) [#5]
Was that you? That's funny. =)


Regular K(Posted 2004) [#6]
i asked about this a few months ago and i never got a good answer!

:)