...I have 20 elements from the data view with random values that change every minute, would like to sort them in descending order to be able to see them in my HMI C-more screen.
Summary
See attached PDF.
It's more or less a naive implementation of bubble sort cf.
this link.
I wrote it for, and tested it in, the Allen-Bradley/CCW/Micro820 environment. There are no complex instructions I would expect to be missing from a Do-More; the algorithm itself is six rungs and about a dozen and a half instructions, so porting it should not take much effort.
Details
Rung 3 has the [
Sort_loop] label that is jumped to from Rung 7; see image below.
Rung 8 (not fully shown) has the [
Bypass_sort] label that is jumped to from Rung 2 or Rung 3.
On a press of the Normally-Closed [arm_sort_button], Rung 1 initializes the sort by making two assignments:
- the value 19 to the current index ([icurrent] DINT) which is the index of the last element in the list of 20 numbers to be sorted;
- the value 1 to the [swapped] BOOL,
After Rung 1 does this Rung 3 will "think" at least one bubble-swap occurred (because [
swapped] is 1) during a just-completed sweep (because the current index is 19) through the list, which Rung 3 will interpret as meaning that
at least one more sweep will be required to sort the list.
Rung 3 acts as the outer loop guard of bubble sort, determining whether the sort is complete i.e. when a complete sweep of the list has been made with no swaps i.e. all the numbers are in decreasing order; if the sort is indeed complete, then the code makes a jump to the [Bypass_sort] label; jumps look like this:
Rung 4 initializes the next inner loop of bubble sort i.e. the next sweep, assigning a 0 to the current index when it is at a maximum, and resetting [
swapped] to 0.
Rung 5 calculates the next index ([
inext] DINT) of the next item after index icurrent.
Rung 6 does the compare, and optionally swaps the values of the next and current indices if the former is greater than the latter.
Rung 7 increments the current index and jumps back to the top of the Sort loop.
Rung 2 is a superfluous guard to allow me to review the unsorted values and trigger the start of the sort.
The second PDF (*_ugly.pdf) is from the "Document Generator" of CCW, in case someone wants to see the full program, but it is very poor quality; is that A-B's fault, or is the summat lacking in my PDF driver?
This effort reinforces my opinion that CCW and Siemens TIA are roughly tied for the Most Awkward IDE award.