# Reverse queue sorting

**URL:** <https://clarionhub.com/t/reverse-queue-sorting/9158>\
**Category:** questions\
**Tags:** queue\
**Created:** [June 11, 2026, 4:40pm UTC](https://clarionhub.com/t/reverse-queue-sorting/9158 "2026-06-11T16:40:19Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Genricke](https://clarionhub.com/user_avatar/clarionhub.com/genricke/32/5271_2.png) [@Genricke](https://clarionhub.com/u/Genricke)\
**Post date:** [June 11, 2026, 4:40pm UTC](https://clarionhub.com/t/reverse-queue-sorting/9158/1 "2026-06-11T16:40:19Z")

</div>

Hi !  
Can you tell me if we can get a function for reverse sorting of an arbitrary queue?  
Thanks !

```clarion
ReverseSort Procedure(*Queue AnyQueue)

```

---

<div class="post-metadata">

**Author:** ![KevinErskine](https://clarionhub.com/user_avatar/clarionhub.com/kevinerskine/32/711_2.png) [@KevinErskine](https://clarionhub.com/u/KevinErskine)\
**Post date:** [June 11, 2026, 5:53pm UTC](https://clarionhub.com/t/reverse-queue-sorting/9158/2 "2026-06-11T17:53:14Z")

</div>

not the way you are thinking but…

this is how you do it

```clarion
Ascending sort
           SORT(Queue, +Queue.Field)
Descending sort
           SORT(Queue, -Queue.Field)

```

---

<div class="post-metadata">

**Author:** ![Genricke](https://clarionhub.com/user_avatar/clarionhub.com/genricke/32/5271_2.png) [@Genricke](https://clarionhub.com/u/Genricke)\
**Post date:** [June 11, 2026, 6:14pm UTC](https://clarionhub.com/t/reverse-queue-sorting/9158/3 "2026-06-11T18:14:58Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jslarve](https://clarionhub.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@jslarve](https://clarionhub.com/u/jslarve)\
**Post date:** [June 11, 2026, 6:25pm UTC](https://clarionhub.com/t/reverse-queue-sorting/9158/4 "2026-06-11T18:25:40Z")

</div>

If it’s simply reversing the order, you could take the gorilla aproach, and loop through the queue, doing your bidding with the records.

---

<div class="post-metadata">

**Author:** ![Genricke](https://clarionhub.com/user_avatar/clarionhub.com/genricke/32/5271_2.png) [@Genricke](https://clarionhub.com/u/Genricke)\
**Post date:** [June 11, 2026, 7:23pm UTC](https://clarionhub.com/t/reverse-queue-sorting/9158/5 "2026-06-11T19:23:27Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Bruce](https://clarionhub.com/letter_avatar_proxy/v4/letter/b/ccd318/32.png) [@Bruce](https://clarionhub.com/u/Bruce)\
**Post date:** [June 11, 2026, 7:34pm UTC](https://clarionhub.com/t/reverse-queue-sorting/9158/6 "2026-06-11T19:34:22Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![jslarve](https://clarionhub.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@jslarve](https://clarionhub.com/u/jslarve)\
**Post date:** [June 11, 2026, 8:07pm UTC](https://clarionhub.com/t/reverse-queue-sorting/9158/7 "2026-06-11T20:07:33Z")

</div>

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:

```clarion
  LOOP Ndx = RECORDS(pQ) TO 1 BY -1
     GET(pQ,Ndx)
     ADD(pQ)
     DELETE(pQ)
  END

```

---

<div class="post-metadata">

**Author:** ![CarlBarnes](https://clarionhub.com/user_avatar/clarionhub.com/carlbarnes/32/1859_2.png) [@CarlBarnes](https://clarionhub.com/u/CarlBarnes)\
**Post date:** [June 11, 2026, 11:37pm UTC](https://clarionhub.com/t/reverse-queue-sorting/9158/8 "2026-06-11T23:37:33Z")

</div>

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?

```clarion
  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

```clarion
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.

---

<div class="post-metadata">

**Author:** ![jslarve](https://clarionhub.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@jslarve](https://clarionhub.com/u/jslarve)\
**Post date:** [June 11, 2026, 11:55pm UTC](https://clarionhub.com/t/reverse-queue-sorting/9158/9 "2026-06-11T23:55:08Z")

</div>

Yeah, I think you’re right about delete first.  
Without ANY, that definitely works fine. Not sure about with.

---

<div class="post-metadata">

**Author:** ![CarlBarnes](https://clarionhub.com/user_avatar/clarionhub.com/carlbarnes/32/1859_2.png) [@CarlBarnes](https://clarionhub.com/u/CarlBarnes)\
**Post date:** [June 12, 2026, 4:09am UTC](https://clarionhub.com/t/reverse-queue-sorting/9158/10 "2026-06-12T04:09:40Z")

</div>

> [@jslarve](#):
>
> Without ANY, that definitely works fine.  
> Not sure about with.

```clarion
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

```

---

<div class="post-metadata">

**Author:** ![vitesse](https://clarionhub.com/user_avatar/clarionhub.com/vitesse/32/837_2.png) [@vitesse](https://clarionhub.com/u/vitesse)\
**Post date:** [June 12, 2026, 6:13am UTC](https://clarionhub.com/t/reverse-queue-sorting/9158/11 "2026-06-12T06:13:03Z")

</div>

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…)

```clarion
  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

```clarion
 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?

---

<div class="post-metadata">

**Author:** ![Genricke](https://clarionhub.com/user_avatar/clarionhub.com/genricke/32/5271_2.png) [@Genricke](https://clarionhub.com/u/Genricke)\
**Post date:** [June 12, 2026, 11:14am UTC](https://clarionhub.com/t/reverse-queue-sorting/9158/12 "2026-06-12T11:14:50Z")

</div>

Thanks for the help!  
I chose the simplest solution.  
Now you can change the sequence of lists and reports without thinking about their contents. 👍

---

<div class="post-metadata">

**Author:** ![CarlBarnes](https://clarionhub.com/user_avatar/clarionhub.com/carlbarnes/32/1859_2.png) [@CarlBarnes](https://clarionhub.com/u/CarlBarnes)\
**Post date:** [June 12, 2026, 3:14pm UTC](https://clarionhub.com/t/reverse-queue-sorting/9158/13 "2026-06-12T15:14:26Z")

</div>

> [@vitesse](#):
>
> 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?

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.
