Sorted Month NamesSometimes you really forget the power of formula language. A friend recently asked about sorting a list of month names into "proper" order. For example, let's say you have a list containing "April" : "December" : "October" : "January". You want to sort this list into chronological order ("January" : "April" : "October" : "December"). This is actually very easy to do in formula language.
Let's say the list is stored in a variable or field called MonthList. To sort that list/field in chronological order, it's three lines in formula language. The first line sets a list of all the month names in the correct order:
AllMonths := "January" : "February" : "March" : "April" : "May" : "June" : "July" : "August" : "September" : "October" : "November" : "December";
The next statement takes the list of months (stored in MonthList), and removes them from the AllMonths list. Basically, Step 1 gives a list of all the month names that are not used.
Step1 := @Trim(@Replace(AllMonths; MonthList; ""));
Now that the code knows the months that were not selected, the next step is to take that sorted list of months and remove all the entries that were not selected. What's left is the months that were selected, in the order that AllMonths specifies (the correct chronological order).
@Trim(@Replace(AllMonths; Step1; ""))
That's it. Note that I'm making the assumption that the original list (MonthList) has all unique entries. The code above will end up with a sorted unique list of months. If someone wants to enhance the code to handle duplicates (so if "May" appears twice in MonthList it will appear twice in the sorted result), you're welcome to email me updated code as I can update this post.