Sorting containers with multiple levels

I encountered a scenario where I wanted to find a record of a certain type somewhere in a big container. This got med thinking, and the result is the code below. I wanted to be able to loop through a container no matter how big it was or how many levels of new containers it contained.

This job searches through the container con1 and sorts the types Integer, Real and String into separate containers. In itself this job might not be very useful, but my goal was to build something that could easily be modified to work in many different cases when looking for different values in containers.

It also finds the specific records of VendTable, CustTable and SalesTable that I hid inside the container structure.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
static void FO_SearchConTypes(Args _args)
{
    container       con1, con2, con3;
    SalesTable      salesTable =
    SalesTable::find("00729_036");
 
    CustTable       custTable  =
    salesTable.custTable_CustAccount();
 
    VendTable       vendTable  =
    vendTable::find("3003");
 
    Common          common;
    container       conOfstrings;
    container       conOfInts;
    container       conOfReal;
    int             j = 1;
    void sortCon(container _con, int _i = 1)
    {
        while (conLen(_con) >= _i)
        {
            switch(typeof(conPeek(_con, _i)))
            {
                case Types::Container :
                sortCon(conPeek(_con, _i));
                break;
 
                case Types::Record    :
                common = conPeek(_con, _i);
 
                Switch(common.TableId)
                {
                    case SalesTable.TableId :
                    salesTable = common;
                    break;
 
                    case CustTable.TableId  :
                    custTable = common;
                    break;
 
                    case VendTable.TableId  :
                    vendTable = common;
                    break;
                }
                break;
 
                case Types::String :
                conOfStrings += [conPeek(_con, _i)];
                break;
 
                case Types::Integer :
                conOfInts += [conPeek(_con, _i)];
                break;
 
                case Types::Real :
                conOfReal += [conPeek(_con, _i)];
                break;
 
                default :
                break;
            }
 
            _i++;
        }
    }
    ;
 
    // I create a couple of containers, and place them
    // so I get three different levels in the main one.
 
    con3 = [5.56, custTable, "Yay!"];
 
    con2 = [9, "Ok!", salesTable, con3, 7.78];
 
    con1 = [1, 2, con2, "Yo!", vendTable];
 
    custTable   = null;
    salesTable  = null;
    vendTable   = null;
 
    // con1 is the container I want to sort.
    // The others will take care of themselves.
    sortCon(con1);
 
    // I Print the newfound records.
    warning(strFmt("custId:  %1", custTable.AccountNum));
    warning(strFmt("vendId:  %1", vendTable.AccountNum));
    warning(strFmt("salesId: %1", salesTable.SalesId));
 
    // After this I just loop through and print the
    // different new containers to the infoLog.
 
    error("Strings -----");
 
    while(conLen(conOfStrings) >= j)
    {
        info(strFmt("%1",
        conPeek(conOfStrings, j)));
 
        j++;
    }
 
    error("Integer -----");
 
    j = 1;
 
    while(conLen(conOfInts) >= j)
    {
        info(strFmt("%1",
        conPeek(conOfInts, j)));
 
        j++;
    }
 
    error("Real -----");
 
    j = 1;
 
    while(conLen(conOfReal) >= j)
    {
        info(strFmt("%1",
        conPeek(conOfReal, j)));
 
        j++;
    }
}

Last 5 posts in Development

Leave a Reply

Your email address will not be published. Required fields are marked *