Hi All,
I am having a list of months shuffled in a list. I want to arrange them in their chronological order. But I can sort only on alphabetical order. Help me guys.
local!months:{"June","January","March","Novemeber","February","October","December","April","July","May","September","August"}, sort(local!months).
Thank you
Discussion posts and replies are publicly visible
What sort of flexibility do you have? Can you make your local variable, instead of holding a list of string, hold a list of dictionary/maps instead, in which you give each month a "name" and a "number" parameter?
Thanks for the reply. Actually my task is that, there will be shuffled list of months and I have to arrange it in chronological order and return the list as output.
Where is the "shuffled list of months" coming from, and in what context? Are you saying that there's a hard requirement that the input is nothing but a text array containing month names, and nothing else?
I am a new joinee and this is for my assignment. This is the question "Add a list of month names in jumbled format. Arrange it in ascending/ desc order".can we give input in some other way?
kavyanatrajan said:Add a list of month names in jumbled format.
Is there any more context than this? This is super unclear.
Sorry about that, but that's what I got. It's not for project case. Just for assignment sake.
Well, assuming you start off with a passed-in, randomized list of month names in a text array, and nothing else, here is what i'd suggest, just off the top of my head:
First, loop over the list of months using a!forEach(). The output of this loop will need to be slightly different dependent on whether we expect it to possibly handle duplicated months. In the loop, compare the month name to a predefined list. The output should be a dictionary or map containing a "name" parameter with the current item name in the loop, then a "number" parameter with the month's integer number in a calendar year (1 - 12).
After that, you should be able to use our standard trick for sorting a dictionary by the value of a particular parameter, sorting by the "number" parameter in this case - utilizing the toDataSubset() function (i believe someone posted the approximate code for this up-thread).
Turns out I can't resist a fun challenge...
a!localVariables( local!orderedMonthNames: a!forEach( enumerate(12), text(todate(fv!index & "/1/2021"), "mmmm") ), local!months: {"June", "January", "March", "Novemeber", "February", "October", "December", "April", "July", "May", "September", "August"}, local!mapped: a!forEach( local!months, a!map( name: fv!item, number: index(wherecontains(fv!item, local!orderedMonthNames), 1, -1) ) ), todatasubset( arrayToPage: local!mapped, pagingConfiguration: a!pagingInfo( startIndex: 1, batchSize: -1, sort: a!sortInfo( field: "number", ascending: true() ) ) ).data )
Me neither -)
a!localVariables( local!orderedMonthNames: a!forEach( enumerate(12), text(todate(fv!index & "/1/2021"), "mmmm") ), local!months: {"June", "January", "March", "November", "February", "October", "December", "April", "July", "May", "September", "August"}, reduce( a!update(_,_,_), {}, merge( a!forEach( items: local!months, expression: lookup(local!orderedMonthNames, fv!item) ), local!months ) ) )
Have an empty array and insert the strings at the correct index.
Huh, I actually didn't know about lookup() - that seems a little more civilized than having to call whereContains() (and flattened, as it were, using index()), when you just want to find a text value within an array.