alphabetic sorting for an enum

I am working on a script to automatically sort the list of entries in an enumeration of Attribute Type in a formal module. The attributes (of the Attribute Type)are in use. I have the feeling that the script is a bit complex:

1) read and copy the enum of Attribute Type to an intermediate array.
2) Save the selections of each object of a mdoule in a temp file.
3) Alphabetically sort the positions in intermediate array.
4) Use modify finction to modify the attribute type
5) Read the temp file and update the attribute selection for each object

Is there any simple alternative solution?

regards, anil.
SystemAdmin - Thu Jun 18 09:29:24 EDT 2009

Re: alphabetic sorting for an enum
llandale - Thu Jun 18 09:54:13 EDT 2009

You won't need to save nor reset Object values.

Go to the DXL manual chapter 'Attributes' section 'Attribute type manipulation' command 'modify (attribute type)'. There is an undocumented version of this command:

AttrType modify(AttrType, string, string], int[, int], string[, int[], string&)

That extra 7th int[] parameter is a mapping of New Enums to the Old enums. So if your old enums are 'Low', 'Medium', and 'High' and you want 'High', 'Low', and 'Medium' the values of that array will be {2, 0, 1} (where position '0' in the array maps to new 'High', and its value 2 maps to 'High' in the old enums list).

So I would read the old Enums, copy to a New enums array, alpha-sort the new array, then for each new sorted value, find its position in the old enums array and build that extra int array. If there are duplicate Enum values then you have a problem.

Now I said you won't 'need' to reset the Object values. Until you do, however, they will display the correct values but in the old order. To get them to display their values in the new order I would do the follow after you modify the type: {for obj in entire mod do; obj.NameAttr = obj.NameAttr}

  • Louie

Re: alphabetic sorting for an enum
Tony_Goodman - Fri Jun 19 04:07:56 EDT 2009

I hacked together the following to sort enums alphabetically.
Be warned that this is not a robust piece of code and needs lots more error checking added to it.

string sortEnumAttrType(string attrTypeName)
{
    Module m = current Module
        int i = 0
        string errmsg = ""
 
        if (null m) return("No current module")
 
        AttrType at = find(current Module, attrTypeName)
 
        if (null at) return("Attribute type not found")
 
        setRealColorOptionForTypes(true)
 
        int atSize = at.size
        int newPos = 0
 
        string new_strings[atSize] 
        int new_values[atSize] 
        int new_colors[atSize] 
        string new_descs[atSize]
        int arrMaps[atSize]
        Skip stringsList = createString
 
        for(i=0;i < atSize; i++) 
        {
                put(stringsList, at.strings[i], i)
        }
 
        newPos = 0
        for i in stringsList do
        {
                new_strings[newPos] = at.strings[i]
                new_values[newPos] = at.values[i] 
                new_colors[newPos] = at.colors[i] 
                new_descs[newPos] = at.descriptions[i] 
                arrMaps[newPos] = newPos
                newPos++
        }
 
 
        at = modify(at, attrTypeName, new_strings, new_values, new_colors, new_descs, arrMaps, errmsg)
 
        return(errmsg)
}
 
 
 
string res = sortEnumAttrType("a")
print res

Re: alphabetic sorting for an enum
IBM_Gust - Fri Jun 19 06:55:57 EDT 2009

Tony_Goodman - Fri Jun 19 04:07:56 EDT 2009

I hacked together the following to sort enums alphabetically.
Be warned that this is not a robust piece of code and needs lots more error checking added to it.

string sortEnumAttrType(string attrTypeName)
{
    Module m = current Module
        int i = 0
        string errmsg = ""
 
        if (null m) return("No current module")
 
        AttrType at = find(current Module, attrTypeName)
 
        if (null at) return("Attribute type not found")
 
        setRealColorOptionForTypes(true)
 
        int atSize = at.size
        int newPos = 0
 
        string new_strings[atSize] 
        int new_values[atSize] 
        int new_colors[atSize] 
        string new_descs[atSize]
        int arrMaps[atSize]
        Skip stringsList = createString
 
        for(i=0;i < atSize; i++) 
        {
                put(stringsList, at.strings[i], i)
        }
 
        newPos = 0
        for i in stringsList do
        {
                new_strings[newPos] = at.strings[i]
                new_values[newPos] = at.values[i] 
                new_colors[newPos] = at.colors[i] 
                new_descs[newPos] = at.descriptions[i] 
                arrMaps[newPos] = newPos
                newPos++
        }
 
 
        at = modify(at, attrTypeName, new_strings, new_values, new_colors, new_descs, arrMaps, errmsg)
 
        return(errmsg)
}
 
 
 
string res = sortEnumAttrType("a")
print res

What was that about 'Getting out more'. I think a case of the pot calling the kettle black.

Re: alphabetic sorting for an enum
RM_Swan - Tue Oct 31 18:29:05 EDT 2017

Tony_Goodman - Fri Jun 19 04:07:56 EDT 2009

I hacked together the following to sort enums alphabetically.
Be warned that this is not a robust piece of code and needs lots more error checking added to it.

string sortEnumAttrType(string attrTypeName)
{
    Module m = current Module
        int i = 0
        string errmsg = ""
 
        if (null m) return("No current module")
 
        AttrType at = find(current Module, attrTypeName)
 
        if (null at) return("Attribute type not found")
 
        setRealColorOptionForTypes(true)
 
        int atSize = at.size
        int newPos = 0
 
        string new_strings[atSize] 
        int new_values[atSize] 
        int new_colors[atSize] 
        string new_descs[atSize]
        int arrMaps[atSize]
        Skip stringsList = createString
 
        for(i=0;i < atSize; i++) 
        {
                put(stringsList, at.strings[i], i)
        }
 
        newPos = 0
        for i in stringsList do
        {
                new_strings[newPos] = at.strings[i]
                new_values[newPos] = at.values[i] 
                new_colors[newPos] = at.colors[i] 
                new_descs[newPos] = at.descriptions[i] 
                arrMaps[newPos] = newPos
                newPos++
        }
 
 
        at = modify(at, attrTypeName, new_strings, new_values, new_colors, new_descs, arrMaps, errmsg)
 
        return(errmsg)
}
 
 
 
string res = sortEnumAttrType("a")
print res

I know this is an old post, but I found this piece of code fairly useful for a large existing enumeration I have to maintain. There is one small bug that causes attributes to change both their values and their position, instead of only the position in the list as desired.

"arrMaps[newPos= newPos" should be "arrMaps[newPos= i" instead.

 

For those wanting to copy paste:

 

string sortEnumAttrType(string attrTypeName)
{
    Module m = current Module
        int i = 0
        string errmsg = ""
 
        if (null mreturn("No current module")
 
        AttrType at = find(current ModuleattrTypeName)
 
        if (null atreturn("Attribute type not found")
 
        setRealColorOptionForTypes(true)
 
        int atSize = at.size
        int newPos = 0
 
        string new_strings[atSize]
        int new_values[atSize]
        int new_colors[atSize]
        string new_descs[atSize]
        int arrMaps[atSize]
        Skip stringsList = createString
 
        for(i=0;i < atSizei++)
        {
                put(stringsListat.strings[i], i)
        }
 
        newPos = 0
        for i in stringsList do
        {
                new_strings[newPos= at.strings[i]
                new_values[newPos= at.values[i]
                new_colors[newPos= at.colors[i]
                new_descs[newPos= at.descriptions[i]
                arrMaps[newPos= newPos
                newPos++
        }
 
 
        at = modify(atattrTypeNamenew_stringsnew_valuesnew_colorsnew_descsarrMapserrmsg)
 
        return(errmsg)
}
 
 
 
string res = sortEnumAttrType("a")
print res