Page 1 of 1
aSort on two fields
Posted: Wed Aug 12, 2015 4:48 pm
by Rick Lipkin
To All
I need to sort an array by two fields .. first by C then by A .. looking at the aSort docs it appears you can only sort on one field and not a concatenation of two. Hopefully I am wrong and someone can give me some advice on sorting this array first on C then on A ..
Code: Select all
aSort( aBitem,,, {|x,y| x[3] < y[3]} )
Thanks
Rick Lipkin
Re: aSort on two fields
Posted: Wed Aug 12, 2015 4:55 pm
by Carles
Rick
Maybe u can try aSort( aBitem,,, {|x,y| x[3] + x[1] < y[3] + y[1]} ). If field 3 is numeric u need to do aSort( aBitem,,, {|x,y| str(x[3],3) + x[1] < str(y[3],3) + y[1]} )
Re: aSort on two fields
Posted: Wed Aug 12, 2015 5:04 pm
by Rick Lipkin
Charles
I thought about adding them together .. but ( for me ) when I did, it did not work .. but your code works !
Thanks
Rick Lipkin
Re: aSort on two fields
Posted: Thu Aug 13, 2015 11:21 am
by sambomb
What type is in these fields?
The best way to do this is converting both to text of the same size.
Ex:
Code: Select all
//-- 1: Code, 2: Last Name, 3: First Name
aBitem := {}
aAdd( aBItem, {1, PadR( "FIELDS", 25 ), PadR("ROBERT",20) } )
aAdd( aBItem, {2, PadR( "BARROW", 25 ), PadR("SARAH",20) } )
aAdd( aBItem, {3, PadR( "FIELDS", 25 ), PadR("ANTHONY",20) } )
//-- Sort by last name
aSort( aBitem,,, {|x,y| ( x[2]+x[3] ) < ( y[2]+y[3] ) } )
? AllTrim( aBItem[1,2] ) + AllTrim( aBItem[1,3] ) //-- BARROW SARAH
? AllTrim( aBItem[2,2] ) + AllTrim( aBItem[2,3] ) //-- FIELDS ANTHONY
? AllTrim( aBItem[3,2] ) + AllTrim( aBItem[3,3] ) //-- FIELDS ROBERT
//-- Sort by first name
aSort( aBitem,,, {|x,y| ( x[3]+x[2] ) < ( y[3]+y[2] ) } )
? AllTrim( aBItem[1,3] ) + AllTrim( aBItem[1,2] ) //-- ANTHONY FIELDS
? AllTrim( aBItem[2,3] ) + AllTrim( aBItem[2,2] ) //-- ROBERT FIELDS
? AllTrim( aBItem[3,3] ) + AllTrim( aBItem[3,2] ) //-- SARAH BARROW
//-- Sort by code
aSort( aBitem,,, {|x,y| ( x[1] ) < ( y[1] ) } )
? AllTrim( aBItem[1,3] ) + AllTrim( aBItem[1,2] ) //-- ROBERT FIELDS
? AllTrim( aBItem[2,3] ) + AllTrim( aBItem[2,2] ) //-- SARAH BARROW
? AllTrim( aBItem[3,3] ) + AllTrim( aBItem[3,2] ) //-- ANTHONY FIELDS
Re: aSort on two fields
Posted: Tue Aug 18, 2015 7:03 am
by Carlos Mora
To avoid dealing with type, a 'type agnostic' comparision function can be used, like
Code: Select all
{|x,y| if( x[3] == y[3], x[1] < y[1], x[3] < y[3] ) }
no type convertion nor concatenation required.
Re: aSort on two fields
Posted: Tue Aug 18, 2015 1:53 pm
by James Bott
Rick,
You could also do it by using a temp DBF (in memory) instead of an array as discussed here:
http://forums.fivetechsupport.com/viewt ... =3&t=31054
You can use an index too.
I haven't tried it yet, so I can't give more specifics.
James
Re: aSort on two fields
Posted: Tue Aug 18, 2015 7:43 pm
by Rick Lipkin
James
Thanks and to everyone else .. I was intrigued with Carlos solution and will give it a try .. for now, adding the two elements together has worked.
Rick Lipkin
Re: aSort on two fields
Posted: Fri Aug 21, 2015 8:38 am
by nageswaragunupudi
Carlos Mora wrote:To avoid dealing with type, a 'type agnostic' comparision function can be used, like
Code: Select all
{|x,y| if( x[3] == y[3], x[1] < y[1], x[3] < y[3] ) }
no type convertion nor concatenation required.
I too use this logic.