The current version of universe programming language I working on does not cater for SORT function. For certain application , there a need read from a record file which contains over 100 items delimitd by value mark, I need to sort by A, by B and by C. Below is the implementation of my code using LOCATE and INS function and for loop.

*FOR EXAMPLE A]B]B]A
*            3]4]3]1
*NEED TO BE SORTED AS
             A]A]B]B
             1]3]3]4
*IN THE FORM OF BY 1ST FIELD, BY 2ND FIELD
SORTED.ARRAY.1=""
UNSORTED.ARRAY.2=""
READ XX.REC FROM XX.FILE.VARIABLE,KEY ELSE
END
1ST.FIELD.TO.SORT=XX.REC<1>
2ND.FIELD.TO.SORT=XX.REC<2>
X=DCOUNT(1ST.FIELD.TO.SORT,@VM)
FOR I=1 TO X
Z1=1ST.FIELD.TO.SORT<1,I>
Z2=2ND.FIELD.TO.SORT<2,I>
LOCATE Z1 IN SORTED.ARRAY.1<1> BY "DR" SETTING POS THEN
     INS Z1 BEFORE SORTED.ARRAY.1<1,POS>
     INS Z2 BEFORE UNSORTED.ARRAY.2<1,POS>
END ELSE
    LOCATE Z1 IN SORTED.ARRAY.1<1> BY "DR" SETTING POS ELSE
     INS Z1 BEFORE SORTED.ARRAY.1<1,POS>
     INS Z2 BEFORE UNSORTED.ARRAY.2<1,POS>
    END
END
NEXT X
*THIS COMPLETE 1ST SORTED ARRAY, NOW NEED TO SLICE THE THE 1ST ARRAY THAT CONSIST OF THE SAME ITEM
*FOR EXAMPLE A]A]B]B] NEED TO CAPTURE THE START POSITION OF "A" AND END POSTION OF "A" THEN SORT THE *2ND FIELD
FOR I2=1 TO X1
    Z1=SORTED.ARRAY.1<1,I2>
    ORIG.START.POS=I2
    FOR I3=ORIG.START.POS TO X+1
        IF Z1#SORTED.ARRAY<1,I3> THEN
            CUT.OFF.POS.OF.SAME.ITEM=I3
            LAST.INDEX.OF.SAME.ITEM=I3-1
            EXIT
        END
    NEXT I3
    TEMP.STORAGE=""
    FOR I4=ORIG.START.POS TO LAST.INDEX.OF.SAME.ITEM
    ITEM.TO.SORT=UNSORTED.ARRAY.2<1,I4>
    LOCATE ITEM.TO.SORT IN TEMP.STORAGE<1> BY "DR" SETTING POS THEN
        INS ITEM.TO.SORT BEFORE TEMP.STORAGE<1,POS>
    END ELSE
        LOCATE ITEM.TO.SORT IN TEMP.STORAGE<1> BY "DR" SETTING POS ELSE
        INS ITEM.TO.SORT BEFORE TEMP.STORAGE<1,POS>
        END
    END
COUNT=1
FOR I5=ORIG.START.POS TO LAST.INDEX.OF.SAME.ITEM
    CUR.ITEM=TEMP.STORAGE<1,COUNT>
    UNSORTED.ARRAY.2<1,I5>=CUR.ITEM
    COUNT=COUNT+1
NEXT I5
ORIG.START.POS=CUT.OFF.POS.OF.SAME.ITEM
NEXT I2

As seen above, just to sort in the order by A follow by B, it already looks like a spaghetti code, and the step is at least O(n^2). Is there a better approach to handle sorting effectively?

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.