PAC Control Code

Has anyone written any code that can “clean up” a table with, let’s say, 50 elements? For Example: I have a table with 50 job numbers. As the jobs are completed, it is deleted from the table. When I add a new job my code seeks out the first spot where the element is empty and places it there. I want to add a button which not only organizes the table by oldest job to newest job or vice versa, but also condense down the table to fill all the first spots. Has anyone written this before? I could eventually get this written, but just looking for some shortcuts.

The table sort (high to low) is here; Search results for 'table sort' - OptoForums

Are the job #'s integers? What do you put in the table to represent “deleted”? I have some sorting subroutines posted on here that may work, depending on what you got.

However, going down the table to find the “empty” spot doesn’t seem like a bad plan - the controller really won’t care :wink: If you want something for the HMI, then sorting from oldest to newest could be helpful.

The Job numbers are Strings. I have 4 string tables that work together. Job Number, Job Name, Job Address and Job Description. I was going to convert the Job Number table to an integer, then find the order, then apply to the four tables to get the order correct. Right now in the string table it looks for the first blank spot ("") with a “for… next” command. -That’s as far as I got and started thinking if I could order these, it would be pretty nice. I guess I could just put them in order the first time manually, then shift elements immediately following a “delete” command and then find the first blank spot when entering a new job (which should put it at the back of the line in the table). -That’s seems more logical to me atm and not even worry if I can switch from newest to oldest and vice versa.

After a little more thinking, I might have to do more than that. At times my boss will give me a job that someone didn’t finish and I’ll have to add it to my tables in the correct order (if I wanted it to be perfect). I’ll have to give it a little more thought.

I’ve been meaning to tweak my sorting routines to allow sorting auxillary tables so I decided to finally finish that - I’ve only completed it for string tables and only have done some basic testing.

Two subroutines attached in 9.3 basic format. I’d prefer if the QuickSortStringBySorted didn’t need an extra utility table, but couldn’t come up with a good way to sort in situ without it - I’m sure there is room for improvement. These were modified from the subroutines I posted here: Faster table sorting with quick-sort algorithm

//stTabletoSortBy should contain the string data that you want all tables to sort by
//nStartIndex and nEndIndex is the range of the table you want to have sorted.
//nStartIndex is most likely 0, and nEndIndex should be the last value in your tables to sort.
//Descending should be 0 for A to Z and 1 if you want to sort Z to A
//ntSortedOrder is an integer table that stores how the string table was sorted to be used in the other subroutine. It should be the same length as the string table.
//ntWorkSortedOrder should be the same length as ntSortedOrder and is a utility table used in the subroutine during the sort.

//This call to QuickSortString_v2 will sort the initial table - the one you want to sort everything by
//and will also setup the ntSortedOrder so we can sort other tables the same way
QuickSortString_v2(stTableToSortBy, nStartIndex, nEndIndex, Descending, ntSortedOrder);
//This will sort the stSiblingTableToSort1 in the same order as stTableToSortBy
QuickSortStringBySorted(stSiblingTableToSort1, nStartIndex, nEndIndex, ntSortedOrder, ntWorkSortedOrder);
//This will sort the stSiblingTableToSort2 in the same order as stTableToSortBy
QuickSortStringBySorted(stSiblingTableToSort2, nStartIndex, nEndIndex, ntSortedOrder, ntWorkSortedOrder);
//Sort additional tables as needed

MultiTableStringQuicksort9.3Basic.zip (9.4 KB)

1 Like

So these subroutines will sort text like I was asking about? I have never used a subroutine yet. Looks like I’ll have to do a little investigating. Thanks philip, I’lll check it out and post when I can.

Yeah, they should, just be aware that what I posted will sort alphabetically, not numerically - so if you have a list of numbers like 7,8,9,10 in a string array, 10 will come before 7 since 1 comes before 7, if that makes sense. I have sort routines for integers, I just haven’t modified them to include the ntSortedOrder table so it can be used to sort other tables.