Page 1 of 1

Asort 101

Posted: Tue Sep 11, 2012 9:43 pm
by Rick Lipkin
To All

I have never had to sort an array before .. I have looked at the asort documentation and looked at some posts in this forum .. and the answer seems a bit 'blurry' to me. :?

I have a simple 4 element array with multiple rows and the last element is the row number assigned to the row when it was added ..

Code: Select all

nRow := oLbx:KeyNo()+1

aLine := { space(50),nRepairNumber,cInventoryId,nRow }
Aadd( aSerial, aLine )

aSort( aSerial, 4 )  // this does not work  

 
the array table may look like this

2345 200001 x3456 3
5678 200001 x3456 1
4567 200001 x3486 2

I want to be able to sort this array by element 4 .. as you can see from my above 'feeble' attempt I need some basic array sorting help.

Any advice would be appreciated!

Rick Lipkin

Re: Asort 101

Posted: Tue Sep 11, 2012 10:37 pm
by Jonathan Hodder
Hi Rick

Real simple and works well.

Syntax -
ASORT(<aTarget>, [<nStart>], [<nCount>], [<bOrder>]) --> aTarget

Example
::aAll:=ASORT(::aAll,,,{|x,y| x[1]<y[1]})

Your code
aSorted := ASORT(aLine,,,{|x,y| x[4]<y[4]})

There are no numeris restrictions either. It takes a few seconds to sort hundreds. Harbour developers did well.
Can supply definitions of syntax if you require them.

Re: Asort 101

Posted: Tue Sep 11, 2012 10:37 pm
by reinaldocrespo
Hi Rick;
Syntax
ASort( <aArray>, [<nStart>], [<nCount>], [<bSort>] ) --> aArray

Arguments
<aArray>
The array to be sorted.
<nStart>
This is a numeric expression indicating the first element in the array to begin sorting with. It defaults to 1, the first element of <aArray>.
<nCount>
A numeric expression specifying the number of elements to sort. It defaults to 1+Len(<aArray>)-<nStart>.
<bSort>
Optionally, a code block defining a sorting rule can be passed. If this parameter is omitted, all values stored in <aArray> are sorted in ascending order. When a code block is specified, it must be declared with two parameters and must return a logical value. The code block receives the values of two adjacent array elements. When the code block returns .T. (true), the first code block parameter is considered smaller than the second. If .F. (false) is returned, the first code block parameter is larger.
Return
The function returns a reference to <aArray>.
Here is sample code:

Code: Select all

aSort( aArray,,, { |x,y| x[1] < y[1] } )
 
Hope that helps,


Reinaldo.

Re: Asort 101

Posted: Wed Sep 12, 2012 12:46 pm
by Rick Lipkin
To All

Thanks .. this seemed to be my solution

aSort( aSerial,,, { |x,y| x[4] < y[4] } )

Thanks
Rick

ps .. be sure you do not use AUTOSORT on your xBrowse ( I did :oops: )