Sorting multidimenzional array

Sorting multidimenzional array

Postby codemaker » Sun Jun 24, 2012 6:34 pm

I have an array like this:
aRezult := { cBillNumber, cName, nYear}

I need to sort on two fields cBillNumber(character element) and nYear (numeric element)
Unsorted data looks like this for example (randomly mixed years and bills):
aRezult := { 4, cName, 2011 }
aRezult := { 1, cName, 2012 }
aRezult := { 2, cName, 2012 }
aRezult := { 5, cName, 2011 }
aRezult := { 3, cName, 2012 }
aRezult := { 4, cName, 2012 }
aRezult := { 1, cName, 2011 }
aRezult := { 2, cName, 2011 }
aRezult := { 3, cName, 2011 }
aRezult := { 5, cName, 2012 }


I know how to sort data in multidimensional array if I want to sort on only one element.
In an example below, I can easily sort by BillNumber for example and get the result like this:
aRezult := { 1, cName, 2011 }
aRezult := { 1, cName, 2012 }
aRezult := { 2, cName, 2011 }
aRezult := { 2, cName, 2012 }
aRezult := { 3, cName, 2011 }
aRezult := { 3, cName, 2012 }
aRezult := { 4, cName, 2011 }
aRezult := { 4, cName, 2012 }
aRezult := { 5, cName, 2011 }
aRezult := { 5, cName, 2012 }


What I need is to sort by BillNumbers inside a year, so the result would be like:
aRezult := { 1, cName, 2011 }
aRezult := { 2, cName, 2011 }
aRezult := { 3, cName, 2011 }
aRezult := { 4, cName, 2011 }
aRezult := { 5, cName, 2011 }

aRezult := { 1, cName, 2012 }
aRezult := { 2, cName, 2012 }
aRezult := { 3, cName, 2012 }
aRezult := { 4, cName, 2012 }
aRezult := { 5, cName, 2012 }

Seems I am too tired today...
Please help
User avatar
codemaker
 
Posts: 208
Joined: Wed Dec 03, 2008 4:48 pm
Location: Belgrade, Serbia

Re: Sorting multidimenzional array

Postby Euclides » Sun Jun 24, 2012 10:01 pm

Bi Boris,
You can add fourth element to the array elements:

aAdd( aRezult[i], str(aRezult[i,3],4)+str(aRezult[i,1],2) )

and the sort it by the fourth element

aOut:= ASORT(aRezult,,, { |x, y| x[4] < y[4] })

HTH
Regards, Euclides
User avatar
Euclides
 
Posts: 156
Joined: Wed Mar 28, 2007 1:19 pm

Re: Sorting multidimenzional array

Postby codemaker » Sun Jun 24, 2012 10:28 pm

Yes, this was something I saw later, after I solved it this way:

aR := aSort( aRezult,,,{|x,y| If( x[3] == y[3], x[1] < y[1], x[3] < y[3] ) } )

As I said, for a moment I lost concentration and was way too tired..

Thanks for help
Boris
User avatar
codemaker
 
Posts: 208
Joined: Wed Dec 03, 2008 4:48 pm
Location: Belgrade, Serbia


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: Google [Bot] and 100 guests