News:

Function Finder  Find all the functions within source code files

Main Menu

I have a problem with sortarray for sorting a string array

Started by cybermind, January 11, 2014, 01:39:27 PM

Previous topic - Next topic

cybermind

I am trying to sort a string array with 830 elements. The program stops when it hits the sortarray command. If I change the last parameter of sortarray to 10, it works.

This is part of my inventory code, the program halts when it touches sortarray :-( Am I doing something wrong? Do you need more data?

if current_in_game_menu_selected$ = "Inventory"
u = 0
dim inventory_list$(830,2)
dim sortable_inventory_list$(830)
for t = 1 to 830
if item_holding(t) > 0
inc u
`transfer item name
inventory_list$(u,1) = item_text$(t,1)
`set pointer to item position in original inventory array
inventory_list$(u,2) = str$(t)
no_items_in_inventory = 0
current_selected_inventory_item = 1
first_item_to_show_in_inventory = 1
`make string for adding correct number of zero's to sortable inventory list number
if t < 10 then zero_string$ = "00"
if t > 9 and t < 100 then zero_string$ = "0"
if t > 99 then zero_string$ = ""
sortable_inventory_list$(t) = item_text$(t,1)+zero_string$
endif
next t
sortarray sortable_inventory_list$(),0,830
i = 1
for t = 0 to 830
if sortable_inventory_list$(t) <> ""
index_in_original_item_array = val(right$(sortable_inventory_list$(t),3))
inventory_list$(i,1) = item_text$(index_in_original_item_array,1)
inventory_list$(i,2) = index_in_original_item_array
inc i
endif
next t
if u = 0
current_selected_inventory_item = 0
no_items_in_inventory = 1
endif
endif


Note for Kevin, if you read this :-) Sorry for posting in that other thread with this subject! I searched for sortarray and ended up in that thread and posted there :-P You can delete my post there if you would like, it just messes up that thread :-P Great work on PlayBASIC! I really like using it! Compiling is really fast and error reports on compile errors are really good!

kevin


Generally it's best you post code people can run,  most won't bother with anything they can't..  The string sorting routine behind SortArray has never been particularly fast, so  I suspect it's the number of strings that chokes the program.  You can definitely write a faster one in just plain old PB.. 





cybermind

Ok, thanks, I will read about sorting algorithms and make one myself :-)

kevin

 There's a number of programs in the source code boards that have sort routines in them.. I suggest looking there.  

Although I can't help wonder what the reason for sort them is ?  Since moving strings around generally isn't the most efficient option.  

  Also, the digits$() function can replace these 3 lines.  

PlayBASIC Code: [Select]
            if t < 10 then zero_string$ = "00"
if t > 9 and t < 100 then zero_string$ = "0"
if t > 99 then zero_string$ = ""





  Sort String Array

PlayBASIC Code: [Select]
Function SortStrings(Me$(),l,r)
p=l: q=r
x$=Me$((l+r)/2)
x$=lower$(x$)
Repeat

While lower$(Me$(p))<x$
p=p+1
EndWhile

While x$<lower$(Me$(q))
q=q-1
EndWhile

If p>q Then Exit

temp$=Me$(q)
Me$(q)=Me$(p)
Me$(p)=temp$

INC p
DEC q

Until q<0

If l<q Then SortStrings(Me$(),l,q)
If p<r Then SortStrings(Me$(),p,r)

EndFunction






 See-> SortArray Documentation