# How to write code to place timer values in order from low to high

**URL:** <https://forum.xod.io/t/how-to-write-code-to-place-timer-values-in-order-from-low-to-high/4454>\
**Category:** XOD Programming Questions\
**Created:** [September 23, 2020, 2:45am UTC](https://forum.xod.io/t/how-to-write-code-to-place-timer-values-in-order-from-low-to-high/4454 "2020-09-23T02:45:07Z")\
**Posts on this page:** 1\
**Showing post:** 14

<div class="post-metadata">

**Author:** ![gweimer](https://avatars.discourse-cdn.com/v4/letter/g/a9adbd/32.png) [@gweimer](https://forum.xod.io/u/gweimer)\
**Post date:** [October 1, 2020, 7:55pm UTC](https://forum.xod.io/t/how-to-write-code-to-place-timer-values-in-order-from-low-to-high/4454/14 "2020-10-01T19:55:06Z")

</div>

I realized a while after I posted that was probably your intent. I didn’t look closely & thought if-else-F was 0, not 8 and for some reason was assuming clock was connected to flip-flop-TGL instead of SET. This should work for your program, but is probably not ideal as a general solution.

One option would be to test if new number is less than old OR old number is equal 0. This would cause 0 to always be replaced by new value, but means 0 will never be stored if it is a valid value (would not be in your case, but for generic min-buffer, it could be a problem).

Another idea I had was to have node store state to know if it has a valid value. This gets more complicated, since you won’t want to pass an invalid value to the next node & you need to remember if value has been stored.

For any of these solutions, something we have over-looked is you probably want a way to reset your buffers so you can start looking for a new set of min/max numbers.

Pretty ugly, and doesn’t add code to reset MEM when output is not VALID (what should it be set to???):

 ![image](https://canada1.discourse-cdn.com/flex027/uploads/xod/original/2X/a/ad29a8a6113a06a7b12e90a2676a5518b327066f.png)  
flip-flop starts as 0/false, indicating MEM is not valid. gate at the bottom prevents CHAIN from sending a pulse if MEM was not valid–we don’t want next node thinking it is getting a valid number. I kept DONE just for completeness…you may need some other action following buffer update even if it didn’t have a valid value.

Abusing casting in C++, you can use OVF value to indicate current value is not valid, removing the need for VLD output pin.  
 ![image](https://canada1.discourse-cdn.com/flex027/uploads/xod/original/2X/c/c369c34a84e4f298c3c656a66e85adc577442c45.png)

```auto
node {
    // Internal state variables defined at this level persists across evaluations
    float ovf = (unsigned int) -1;

    void evaluate(Context ctx) {        
        if (isSettingUp() || isInputDirty<input_RST>(ctx)) {
            emitValue<output_MEM>(ctx, ovf);
            emitValue<output_PASS>(ctx, ovf);
        }
        if (isInputDirty<input_PUSH>(ctx)){
            auto New = getValue<input_NEW>(ctx);
            auto Old = getValue<output_MEM>(ctx);
            if (Old == ovf || New < Old) {
                emitValue<output_MEM>(ctx, New);
                emitValue<output_PASS>(ctx, Old);
            } else {
                emitValue<output_MEM>(ctx, Old);
                emitValue<output_PASS>(ctx, New);
            }
            if (Old != ovf) {
                emitValue<output_CHAIN>(ctx, true);
            }
            emitValue<output_DONE>(ctx, true);
        }
    }
}

```

Comparing min-buffer with min-buff-code after 2 pushes:

 ![image](https://canada1.discourse-cdn.com/flex027/uploads/xod/original/2X/8/8e15860b05271f6276c0e86806fc20584001c3e4.png)

---

_[View the full topic](https://forum.xod.io/t/how-to-write-code-to-place-timer-values-in-order-from-low-to-high/4454)._
