Sorting enumerations

3
When you have enumerations in multiple languages you want to be able to sort the enumeration based on the language chosen. In the modeller when you do a xpath retrieve like [CountryEnumeration = 'Netherlands'] you will see that when selecting Netherlands the country will be translated accoring to the setting and I see the list sorted. But now the front end. If I log into the application and select the country the sorting is not applied. To the front end user the list is presented not sorted based on the user language. How can I achieve the same as in the modeller? It is strange to me that as a programmer I get to see the sorted list based on my language setting but the user of the program will see a different list while having the same language setting as the programmer. Regards, Ronald [EDIT] Here is a screenshot of when I work as modeller.
asked
3 answers
2

Interestingly, I ran into exactly the same problem last week. I fixed it in the following way:

  • Create a non-persistent entity with a string attribute and an association to the object containing the enumeration on which to sort (in my case these were reasons to populate a drop down)
  • Retrieve the relevant reasons
  • Loop over the reasons, create a non-persistent entity, use getCaption() to populate the string of the non-persistent entity and pointing the association to the reason and add it to a list
  • Use a sort action on the list of non-persistent entities, sorting on the string attribute
  • Loop over the now sorted non-persistent entities, retrieve the associated object and put it in a new list
  • Return the sorted output list

Example

It's terribly inefficient, but a discussion on my company Sprintr hasn't resulted in a better solution yet. I'm still interested in a better solution though.

answered
2

The ideal situation for us is to sort on the enum values in the order they are defined in the model. Unfortunately this is not the case at the moment but that is a situation we want to go towards.

This is the ideal scenario in most cases. For example, consider enums with:

  • Days of the week
  • Months of the year
  • Low/Medium/High

None of these should be sorted lexicographically, neither on the caption in whatever language, nor the actual value.

Of course, to sort lexicographically based on the value and not the caption, all you'd have to do is order them in that way, so you could still do that even in that situation.

Sorting an enum on the caption of a specific language is not likely to be implemented though, not only would all the translations have to be stored in the database, but you would also need additional joins in your queries in order to sort correctly, resulting in a performance loss.

For your situation, wouldn't it be better to have the countries as an entity instead of an enum? Even though new countries don't often get created, it would seem convenient to be able to add them without having to make model changes. Also I don't know what you use them for but it doesn't seem likely you're selecting them that often that you really need the performance of an enum.

answered
0

Hi Ronald,

Just a vague response or idea. This is not much.

if i understand your problem, then you want to display values of the enumeration as sorted, based on the chosen locale/language. Hence i guess there would be way to do that. As in java lets say you have a list of strings in german and you want to display them in sorted order, then in order to do that we use Collator class get the instance on that specific locale and then sort.

i dont know whether that is possible with mendix enum or not. As per my understanding they are basically 2 D arrays.

answered