Werner :)

Well I've been thinking about it for a couple of hours now but am afraid I'm blocked - the best I can come up with is *ugly*! Hopefully some better MUMPSters (oops!) out there will have better ideas...

You have

set a(1) = 6
set a(2) = 4
set a(3) = 3
set a(4) = 1
set a(5) = 3

and whilst your idea of using the values as subscripts and have Cach� sort them is natural it does run on the issue of duplicate values... Which leads me to the "naive" solution of doing this:


sorted(1,0)=""
sorted(3,0)=""
sorted(3,1)=""
sorted(4,0)=""
sorted(6,0)=""

i.e. adding an additional subscript to prevent overwriting entries. To get the sorted "a" from "sorted", you can traverse the latter with $Query and use $Qsubscript to retrieve the first subscript as the value to set:

Kill sorted
Set i=""
For  {
  Set i=$Order(a(i))
  If i="" Quit
  //
  Set val=a(i)
  Set seq=$Order(sorted(val,""),-1)+1
  Set sorted(val,seq)=""
}
// Rebuild a
Kill a
Set i=1
Set node=$Query(sorted)
While node '= "" {
  Set val=$Qsubscript(node,1)
  Set a(i)=val
  Set i=$Increment(i)
  Set node=$Query(@node)
}

==================================================================

Yes... That's as ugly as it gets. An alternative could be using the values of the "sorted" arrays as repetition indicators, i.e.:

sorted(1)=1
sorted(3)=2
sorted(4)=1
sorted(6)=1

The implementation of the code after "rebuild a" above is left as an exercise to the reader :)

So there, I posted it... I am sure I will now come up with a cleaner idea and hate me a bit for a few days :)

(I've been working on and off in a "Collections" class for a ZCL project which is currently idle, it's basically a set of methods for .e.g converting between e.g. %ArrayOfDataTypes and COS local arrays, etc. ... Searching and sorting could be added as well. But I don't know if/when will I be able to resume work on that... )

HTH,

Ram�n

--
ZCacheLib - Open Source Extensions for Cach�
http://www.zcachelib.org



Reply via email to