Order of Attrributes in Entity

0
I am exporting a non persistent entity to excel using java. I am getting the rows for each record as follows: for (IMendixObject obj : exportList){ int j = 0; Map<String,? extends IMendixObjectMember> mxObj = obj.getMembers(this.context); for (Map.Entry<String,? extends IMendixObjectMember> mxObjEntry : mxObj.entrySet()){ IMendixObjectMember mxObjMember = (IMendixObjectMember) mxObjEntry.getValue(); IExcelColumn col = excelGrid.getColumn(j); IExcelCell cell = col.getCell(i); if (mxObjMember.getValue(context) != null){ cell.setContent(mxObjMember.getValue(context).toString()); } else{ cell.setContent(""); } if (!mxObjMember.getName().trim().equalsIgnoreCase("") && mxObjMember.getName() != null){ j++; } } i++; } Is there any way to force the order of the column to be in the same order that they appear in the modeller's entity model or any other order
asked
1 answers
1

In general terms, a key set in a Java Map is unordered. You could impose an ordering on a set by copying its contents to a SortedSet such as TreeSet, or in case of a Map use a TreeMap. Pass it a Comparator<imendixobjectmember> in the constructor that compares two members and returns a negative, 0, or positive value accordingly.

Something like this:

Comparator comp = new Comparator<String>() {
    public int compare(String s1, String s2) {
         return s1.compareTo(s2); // lexicographic order
    }
}

SortedMap<String,? extends IMendixObjectMember> mxObj = new TreeMap<String,? extends  IMendixObjectMember>(comp);
mxObj.putAll(obj.getMembers(this.context));
for(String key : mxObj.keySet()) {
     IMendixObjectMember mxObjMember = mxObj.get(key);
    // fill cell contents
}

I doubt there is a way of reconstructing the order from the entity model.

answered