Michael, I finally got a chance to look at this. I came up with something using the BubbleSort algorithm, but it's not very pretty. I basically took the array containing all the tokens on the page and filtered for range and npc_condition_immunites, but instead of populating an inRange array, I populated three indexed variables for hp, name, and tokenid (so hp0, hp1, name0, name1, etc). Then edited the BubbleUp procedure to do the index swapping for all three variable sets. You could add or subtract any variables you don't need (like probably name), but I figured you would need tokenid to do your condition marker. If you remove name, be sure to remove the corresponding block from the BubbleUp procedure. Currently I am just outputting the varaible sets before and after sorting, but you should be able to easily add the stuff you want to do with the sorted values. !script {{
--#title|Sorting values from arrays Example
--:(1) GET ALL TOKENS INTO THE "allTokens" ARRAY| will have blank 1st element to be removed later
--~|array;pagetokens;allTokens;@{selected|token_id}
--:(2) CREATE THE "inRange" ARRAY TO HOLD TOKENS IN RANGE|
--~|array;define;inRange;
--:(3) PREP ARRAY FOR LOOP| if no array elements then end macro
--~tokenid|array;getfirst;allTokens
--?[&tokenid] -eq ArrayError|endOutput
--:(4) FIND ALL TOKENS IN RANGE| add indexed roll variables for tokID, name, & hp
--=numToks|0
--=i|0
--+~~~Tok Order BEFORE sorting~~~|
--:RangeLoop|
--:TOKEN MUST BE ON OBJECTS OR GMLAYER AND TYPE MUST INCLUDE UNDEAD|
--?[*[&tokenid]:t-layer] -ne objects -and [*[&tokenid]:t-layer] -ne gmlayer|NextToken
--?"[*[&tokenid]:npc_condition_immunities]" -inc "sleep" -or [*[&tokenid]:t-bar1_value] -lt 1|NextToken
--:CHECK DISTANCE IN UNITS. 20ft is 4UNITS|
--~dist|euclideandistance;@{selected|token_id};[&tokenid]
--?[$dist] -gt 4|NextToken
--:ADD TO THE "ARRAYS" OF VARIABLES|
--&tokID[$i.Total]|[&tokenid]
--&name[$i.Total]|[*[&tokenid]:t-name]
--=hp[$i.Total]|[*[&tokenid]:t-bar1_value]
--=numToks|[$numToks] + 1
--:OUTPUT PRE_SORTED VALS|
--+Tok[$i.Total]|[&name[$i.Total]], [$hp[$i.Total]], [&tokID[$i.Total]]
--=i|[$i] + 1
--/~|array;add;inRange;[&tokenid]
--:NextToken|
--~tokenid|array;getnext;allTokens
--?[&tokenid] -ne ArrayError|RangeLoop
--:(5) SORT ALL "ARRAY" VARIABLES BY HP VALUE WITH A BUBBLE SORT|
--=max_i|[$i]
--=i|0
--:OuterLoop|
--=i|[$i]+1
--=j|[$i]
--:InnerLoop|
--=j|[$j]+1
--?[$hp[$i.Total]] -gt [$hp[$j.Total]] |>BubbleUp;[$i.Total];[$j.Total]
--?[$j.Total] -lt [$NumDice.Total]|InnerLoop
--?[$i.Total] -lt [$max_i.Total]|OuterLoop
--:(6) OUTPUT THE SORTED VARIABLES|
--=i|0
--+~~~Tok Order AFTER sorting~~~|
--:SortLoop|
--+Tok[$i.Total]|[&name[$i.Total]], [$hp[$i.Total]], [&tokID[$i.Total]]
--=i|[$i]+1
--?[$i.Total] -lt [$numToks.Total]|SortLoop
--:End|
--X|
--:PROCEDURES|
--:BubbleUp| accepts i, j as parameters. Swaps var[i] & var[j] for three variables
--=Temp|[$hp[%2%]]
--=hp[%2%]|[$hp[%1%]]
--=hp[%1%]|[$Temp]
--&Temp|[&tokID[%2%]]
--&tokID[%2%]|[&tokID[%1%]]
--&tokID[%1%]|[&Temp]
--&Temp|[&name[%2%]]
--&name[%2%]|[&name[%1%]]
--&name[%1%]|[&Temp]
--<|
}} Sample output: