Alphabet sort

BlitzMax Forums/BlitzMax Beginners Area/Alphabet sort

(tu) ENAY(Posted 2006) [#1]
Anyone have any tips of ideas on how to sort a list of strings into alphabetical order.

At the moment, I'm doing it by comparing the ascii values of each bit of the string, and then quicksorting them into value order.

It's sort of working, but it struggles when you put stuff like "aaaa", "aaa" in there and also stuff liked "plop 1", "plop a", "plop b".

Surely there's a better way? :)


CS_TBL(Posted 2006) [#2]
Doesn't TList have a sort method? :P


TomToad(Posted 2006) [#3]
You could do If SomeString.ToLower() > OtherString.ToLower() Then...


Justus(Posted 2006) [#4]
Or simply use SortList() :)


TomToad(Posted 2006) [#5]
The problem with SortList() is that it will sort based on ASCII value rather than alphabetical. Meaning that strings that are capitalized will appear before strings which are lowercase. Using quicksort with my version of compare will sort the strings by alphabet regardless of case.


Chris C(Posted 2006) [#6]
I'd override the compare method of a string wrapper custom type and use mystringtypelist.sort()


(tu) ENAY(Posted 2006) [#7]
Oh, Blitzmax has a sorting method?
Anyone have any sample code? I'm not exactly tooled up on TLists. :)


Chris C(Posted 2006) [#8]
LOL theres loads of people throwing their rattles out of the pram about the compare method at the moment!!

have a look at my site theres a list tutorial there


CS_TBL(Posted 2006) [#9]
arrays have sort too!

Local cheese$[10]
Local c:Int=0

cheese[c]="Wasn't";c:+1
cheese[c]="ENAY";c:+1
cheese[c]="the";c:+1
cheese[c]="one";c:+1
cheese[c]="who";c:+1
cheese[c]="likes";c:+1
cheese[c]="cheese?";c:+1

cheese.sort()

DebugLog "case sensitive sorted:"
For c=0 To 9
	DebugLog cheese[c]
Next

End


(run @ debugmode)


(tu) ENAY(Posted 2006) [#10]
Wow CS_TBL! I want your babies!


FlameDuck(Posted 2006) [#11]
Or simply use SortList()
Problem is, it uses a bubblesort implementation (quadratic) where as a quicksort (n log n) is much faster.