a litte trouble
im attempting a tactics type RPG & am uneducated about how to
"sort a list by there movement variable"
any insight?
thanks for the help i will go ahead & give this a shot
if no avail :/ then ill have to come back |
In response to Magicsofa
|
|
This sort is a selection sort, which is an easy one to implement (I used to favor it myself way back in the day) but might be one of the worst for performance.
For small lists, your best choice is a binary insertion sort. For larger lists, you'd want to do a quicksort or a merge sort. Here's just a binary insertion sort: // Sort in ascending order by the "value" var Since the number of players should seldom top 100, although 200 is not unheard of for some games, this should do fine. For larger lists, I would suggest a quicksort or merge sort approach, falling back on binary insertion sort when sorting a smaller section. Quicksort is unstable; merge sort and insertion sort are stable. Here's an example of a dual-pivot quicksort falling back on binary insertion sort. I'm leaving out the var reads and just sorting by value in this example. // Sort L in ascending order using a quicksort or binary insertion |
Bascially there are two 'cursors' (i and j) and a max value (hi). Each j loop looks through the remaining list for something that scores higher than i. If you're not sure what's going on I recommend using sleep() and some output on each iteration so that you can see it working.