Thu Dec 02, 2021 7:43 am
Login Register Lost Password? Contact Us


Sort on Enumvalues rather than data.

Questions around writing code and queries

Wed Apr 21, 2021 9:01 am Change Time Zone

Hi ALL ,
In case have to sort on enum values rather the column values as need is in specific order
and which has to be consistent and in control rather to be dependent on data.
Is there a way to sort

MyRec := {STRING3 Value1,STRING3 Category1, STRING3 Category2};
SomeFile := DATASET([{'NVS','T','ABC'},{'NVS','C','ABC'},{'A','X','BAC'},
{'B','G','PQR'},{'A','B','AAC'},{'NVS','T','AAA'}],MyRec);

srtvalues := enum(unsigned1,AAA, ABC, BAC,AAC,PQR);
//need to sort on values of 3rd field which is static but can add latter
Srtd_values_ds := sort(SomeFile,srtvalues.AAA);
harshdesai
 
Posts: 26
Joined: Fri Feb 23, 2018 8:30 am

Mon Jun 21, 2021 9:29 am Change Time Zone

Hi,
You need a mapping between your codes and a sort order.
This can be achieved using a lookup, e.g. DICTIONARY.
Something like this:
Code: Select all
MyRec := {STRING3 Value1,STRING3 Category1, STRING3 Category2};
SomeFile := DATASET([{'NVS','T','ABC'}
                    ,{'NVS','C','ABC'}
                    ,{'A','X','BAC'}
                    ,{'B','G','PQR'}
                    ,{'A','B','AAC'}
                    ,{'NVS','T','AAA'}],MyRec);

SortOrder := DATASET([{'AAA',1},{'ABC',2},{'BAC',3},{'AAC',4},{'PQR',5}],{STRING Code,UNSIGNED1 SortOrder});

Dict      := DICTIONARY(SortOrder,{Code => SortOrder});

Srtd_values_ds := sort(SomeFile,Dict[Category2].SortOrder);
Srtd_values_ds;


Does this fit your needs?
Allan
Allan
 
Posts: 442
Joined: Sat Oct 01, 2011 7:26 pm


Return to Programming

Who is online

Users browsing this forum: No registered users and 1 guest