FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Sorting multidimenzional array
Posts: 208
Joined: Wed Dec 03, 2008 04:48 PM
Sorting multidimenzional array
Posted: Sun Jun 24, 2012 06: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

Posts: 159
Joined: Wed Mar 28, 2007 01:19 PM
Re: Sorting multidimenzional array
Posted: 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
Posts: 208
Joined: Wed Dec 03, 2008 04:48 PM
Re: Sorting multidimenzional array
Posted: 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

Continue the discussion