TIP: Click on subject to list as thread! ANSI
echo: clipper
to: ALL
from: ERIK WACHTMEESTER
date: 1997-08-18 19:34:00
subject: Codeblocks and arrays

Reply-To: erik.wachtmeester@bighole.iaf.nl
following up a message from Erik Wachtmeester to All:
 EW> Is it possible to use the asort-way of using a codeblock (something
 EW> like { |x,y| x[nOffset] < y[nOffset] } ) in a UDF that manipulates
 EW> an array about the same way asort() does? 
Never mind me asking, I wasn't thinking straight, I guess... ;-)
It's actually quite easy to write a brute force bubblesort routine using a
codeblock (and that was what I was looking for) :
<---------
// universal array bubblesort
function x_asort( aInput, nStart, nEnd, bExpression )
  local aReturn, nCount, lSorted, aSwap
  // init local vars
  aReturn := aInput
  lSorted := .f.
  // adjust missing parameters
  nStart  := if( empty( nStart ), 1, nStart )
  nEnd    := if( empty( nEnd ), len( aReturn ), nEnd )
  // check sort state
  do while !( lSorted )
    // set sorted flag
    lSorted := .t.
    // run array
    for nCount := nStart to nEnd - 1
      // evaluate codeblock on array elements
      if !eval( bExpression, aReturn[ nCount ], aReturn[ nCount + 1 ] )
        // no success, so swap array elements
        aSwap                 := aReturn[ nCount ]
        aReturn[ nCount ]     := aReturn[ nCount + 1 ]
        aReturn[ nCount + 1 ] := aSwap
        // reset sorted flag
        lSorted := .f.
      endif
    next
  enddo
return ( aReturn )
--------->
Take care though: while an "aArray := asort( aArray,,, { |x,y| x[4] < y[4] } 
)"
will work flawlessly, with x_asort this can end up in an endless loop if x[4]
and y[4] are exactly the same. This can be remedied by using "aArray := 
x_asort(
aArray,,, { |x,y| x[4] <= y[4] } )".
Regards,
Erik 
---
---------------
* Origin: May it be on this earth? (2:283/7.2)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.