Hi !
Can you tell me if we can get a function for reverse sorting of an arbitrary queue?
Thanks !
ReverseSort Procedure(*Queue AnyQueue)
Hi !
Can you tell me if we can get a function for reverse sorting of an arbitrary queue?
Thanks !
ReverseSort Procedure(*Queue AnyQueue)
not the way you are thinking but…
this is how you do it
Ascending sort
SORT(Queue, +Queue.Field)
Descending sort
SORT(Queue, -Queue.Field)
Thank you for participating!
But unfortunately, we don’t have any information about the fields in the queue.
I just need to reverse the order of the records instead of sorting them by fields.
If it’s simply reversing the order, you could take the gorilla aproach, and loop through the queue, doing your bidding with the records.
I’m sorry, but I don’t understand.
There is only one requirement - to reverse the order of entries. Is it possible to see an example?
I think you need to tell us more. A queue is a queue. You can loop through it in any order.
Are you looping through it yourself, or using z list box to display it?
Since this is not based on some kind of values of some unknown fields, you can’t use the functional sorting feature of SORT(). But you can loop through the queue.
Pseudo code:
LOOP Ndx = RECORDS(pQ) TO 1 BY -1
GET(pQ,Ndx)
ADD(pQ)
DELETE(pQ)
END
Help says you will Delete your ADD:
DELETE (delete a record)
DELETE(queue) removes the QUEUE entry at the position of the last successful GET or ADD and de-allocates its memory.
So Delete first?
LOOP Ndx = RECORDS(pQ) TO 1 BY -1
GET(pQ,Ndx)
DELETE(pQ) !Delete the GET'd record
ADD(pQ) !Add to the end
END
That code looks good. An alternative might be to build a Temp Queue
ReverseSort Procedure(*Queue AnyQueue)
TempQ QUEUE
STRING(SIZE(AnyQueue))
END
Ndx LONG,AUTO
CODE
LOOP Ndx = RECORDS(AnyQueue) TO 1 BY -1
GET(AnyQueue,Ndx)
TempQ = AnyQueue
ADD(TempQ)
END
FREE(AnyQueue)
LOOP Ndx = 1 TO RECORDS(TempQ)
GET(TempQ,Ndx)
CLEAR(AnyQueue) !Incase there is an ANY
AnyQueue = TempQ
ADD(AnyQueue)
END
If the Queue has ANY fields I’m not sure what will happen, there could be problems. I added a CLEAR(Q) for ANY but since the actual Queue structure is unknown that probably does nothing.
Yeah, I think you’re right about delete first.
Without ANY, that definitely works fine. Not sure about with.
LOOP Ndx = RECORDS(pQ) TO 1 BY -1
GET(pQ,Ndx)
ADD(pQ) !add to end
GET(pQ,Ndx) !reget else would delete add
DELETE(pQ)
END
Carl, wouldn’t your two code snippets both work? (It is not clear to me whether you were suggesting a correction or merely an alternative…)
LOOP Ndx = RECORDS(pQ) TO 1 BY -1
GET(pQ,Ndx)
DELETE(pQ) !Delete the GET'd record
ADD(pQ) !Add to the end
END
and
LOOP Ndx = RECORDS(pQ) TO 1 BY -1
GET(pQ,Ndx)
ADD(pQ) !add to end
GET(pQ,Ndx) !reget else would delete add
DELETE(pQ)
END
I’m not sure the extra get is necessary as DELETE does not affect the buffer so the first version should work fine. I’m not clear how ANY would affect anything here?
Thanks for the help!
I chose the simplest solution.
Now you can change the sequence of lists and reports without thinking about their contents. ![]()
I was worried the Delete may dispose the ANY’s (but help says no). AFAIK this procedure has no idea what’s in the Queue, it’s just a big STRING so should be no problems.
ANY’s in a Queue has potential for a GPF or leaks if not handled properly. See the help on ANY below its a good to know:
ANY (any sim> ple data type)
When an ANY variable is declared in a QUEUE structure, there are some special considerations that must be followed. This is due to the internal representation of an ANY and its polymorphic characteristics.
Use of CLEAR() and reference assignments with QUEUE entries.
Once an ANY variable in a QUEUE has been assigned a value, another simple assignment statement will assign a new value to the ANY. This means the previous value is replaced by the new value. If the first value has already been added to the QUEUE, then that entry will “point at” a value that no longer exists.
Once an ANY variable in a QUEUE has been reference assigned a variable (AnyVar &= SomeVariable), another reference assignment statement will assign a new variable to the ANY. This means the previous “pointer” is disposed of and replaced by the new “pointer.” If the first reference has already been added to the QUEUE, then that entry will “point at” a “pointer” that no longer exists.
Therefore, Queue fields of type ANY must be set to NULLs by executing the CLEAR statement with the QUEUE record structure itself as parameter, i.e. the program must execute CLEAR(Queue), before setting new values of queue field of type ANY for the next ADD() or PUT(). This is because CLEAR for QUEUEs and GROUPs are not applied recursively to the data pointed at from fields of reference types. Therefore, CLEAR(queue) just sets fields of type ANY to NULLs without disposing of their internal data.
In addition, you need to reference assign a NULL to all queue fields of type ANY (Queue.AnyField &= NULL), prior to deleting the QUEUE entry, in order to avoid memory leaks.