This might give you some ideas. Here's a sub that will locate all requested items of item type %bla and return them in ascending order in the following variables:
%index = number of items found
%findx0, %findy0, %findz0, %finddist0
%findx1, %findy1, %findz1, %finddist1
%findx2, %findy2, %findz2, %finddist2
...
etc = locations of items and their distances, all in ascending order
sub FindObjects
set %index 0
ignoreitem reset items
for %i 1 23
{
finditem %bla G_ , %i
if #FINDCNT > 0
{
for #FINDINDEX 1 #FINDCNT
{
set %findx . %index #FINDX
set %findy . %index #FINDY
set %findz . %index #FINDY
set %finddist . %index #FINDDIST
set %index %index + 1
ignoreitem #FINDID items
}
}
}
return
I'm not sure if this is exactly what you want, but I whipped it up for you anyhow. Perhaps it'll give you something else to think about.

The only minus about this code is that it does do a bunch more finditem calls. No real biggy because the code is now more elegant and easier to understand.