Interactive PowerBasic Forum

IT-Consultant: Charles Pegge => OxygenBasic Examples => Topic started by: Charles Pegge on February 16, 2025, 11:12:31 AM

Title: ReIndex Macro for Filtering and Sorting Data
Post by: Charles Pegge on February 16, 2025, 11:12:31 AM
The reindex macro uses a merge-sort algorithm, one if the fastest. You can use any criteria for this operation by setting up filter and compare macros. They are equivalent to callbacks from reindex, but do not have the overheads associated with functions:

'filtering and indexing
'======================
uses console
'
'SOME DATA:
===========
dim string s=
{
  "socks","carrots","oranges","desks",
  "zebra","grapes","eternity","arrow","yurt"
}
print ubound(s) cr
'
'SETUP FILTERING AND SORTING SPEC

macro filter(ok,a)
==================
if asc(s[a])>=0x79 'yz'
  ok=0
else
  ok=1
endif
end macro
'
macro comparator(swap,a,b)
==========================
'ascending order
if s[a]>s[b] then swap=1
end macro
'
'BUILD INDEX
'
reindex (idx,ubound(s),count,filter,comparator)


'DISPLAY RESULT USING IDX

print cr count cr
int i,j
for i=1 to count
  j=idx[i]
  print s[j] cr
next
del idx 'when done
wait