Asort 101

Post Reply
User avatar
Rick Lipkin
Posts: 2397
Joined: Fri Oct 07, 2005 1:50 pm
Location: Columbia, South Carolina USA

Asort 101

Post 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
Jonathan Hodder
Posts: 77
Joined: Sun Aug 26, 2007 11:53 pm

Re: Asort 101

Post 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.
User avatar
reinaldocrespo
Posts: 918
Joined: Thu Nov 17, 2005 5:49 pm
Location: Fort Lauderdale, FL

Re: Asort 101

Post 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.
User avatar
Rick Lipkin
Posts: 2397
Joined: Fri Oct 07, 2005 1:50 pm
Location: Columbia, South Carolina USA

Re: Asort 101

Post 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: )
Post Reply