### sorting a list?(solved)

#### jaybdemented

• Posts: 35
I need a way to sort enemy's on a list based on their distance for the actor.  so the enemy within the region that has the greatest distance would be the first on the list followed by the second and so on.  I know how to get the distance and how to add it to the list. what I need is a easy way to to sort them from largest to smallest. This is going in the always block because is changing all the time.

« Last Edit: May 08, 2013, 11:51:20 am by jaybdemented »

#### Innes

• Posts: 1961
As I understand it, there's no native list sorting available in Stencyl, so a manual sort will be required.

If you need some ideas for the logic to achieve this, let us know.
Visit www.TheStencylBook.com - the only published book for learning Stencyl.

#### jaybdemented

• Posts: 35
Yeah. I did search the forums a bit and notice that also. so any help you can give would be great.

#### MyChairHasALooIn

• Posts: 310
you need a floating value to store entries that are being moved, and a boolean attribute which i shall call changes made.

repeat (number of items in list - 1) times.
if get item (current loop count) from list > get item (current loop count + 1)
set float to get item (current loop count) from list
replace item (current loop count) with (get item (current loop count +1)) in list
replace item (current loop count +1) with float in list

the example assumes that the value being compared is distance and that you want smaller values to the front of the list.

when this function completes, your list should be sorted, I do understand that your list is probably more complex than just a list of distances, but I'm assuming you know how to access the relevant bits of information you need

I personally prefer to use lists of lists, that way the shifting of places within lists is much simpler copy list, replace list, replace list, shazzam,

#### jaybdemented

• Posts: 35
thanks for the help.  just a few changes and that should work for what I need.

#### MyChairHasALooIn

• Posts: 310
I'm really glad you found my terribly written help helpful, if you need any more help feel free to PM me, problem solving is my favourite use of my time, which is probably why all my games look like crap, as I tend to lose interest once I've worked out how to do a thing.