Is it possible to sort on a specific field of an array? I'm trying to do an alphabetized list of immunizations with dates, but this sort seems to be sorting on the first field, ImmunizationId, which doesn't mean anything to the reader. Can I tell it to sort by the third field, VaccineGroupName, instead?
local a = getfield(IMMUN_GETLIST("", "last"), "|", "")
local b = sort(a, TRUE)
I am sure there is some more elegant way of doing this but here is what I have done before. First I loop through each entry and prepend the field you want to sort by to the beginning of the string followed by a delimiter.
local immList = getfield(IMMUN_GETLIST(“”, “last”), “|”, “”)
for i =1, i <= size(immList), i = i + 1 do
local immDetails = getfield(immList[i],"^","")
immList[i] = immDetails[3] + "^" + immList[i]
endfor
immList = sort(immList, TRUE)
You can either use that array as is (and add 1 to every entry in your code where you are referencing a specific field) or you can get rid of the first element. To get rid of it do something like this.
for j = 1, j <=size(immList), j=j+1 do
details = getfield(immList[j], "^", "")
immList[j] = sub(immList[j], size(details[1])+2)
endfor
This is untested code. I have tested this method of sorting, however to sort the problems list by problem name and by the problem last assessed date.
Breaking down the array, rebuilding it so that the sort item is index position 1, then sorting as suggested is the more efficient method and involves the least amount of MEL code. There are, however, some caveats based on how sort works. Specifically, understand that sort will first sort all upper case characters, then lower case characters making it possible to have a list that displays the A-Z elements followed by the a-z elements.
i.e.: Apples, Pears, oranges, peaches
Not ideal by any stretch.
To get a proper sort, you will need to perform numerous functions where an index is assigned to each sub array, the 'sort item' is made a single case, sorted, and the array rebuilt. This could be accomplished with multiple loops or by using the insert(array) data symbol. Either way, it is a process that requires more code than one would expect.
Based on what Lee said in his post, if you want to make sure it sorts properly even if there is mixed case, change the line in the first for loop to:
immList[i] = toupper(immDetails[3]) + "^" + immList[i]
Thank you both. I'm going to try adapting your code to see if I can make it work on a test form. It certainly would be a lot easier if sort() took another parameter to tell it which value to use.
Agreed. Sadly, MEL is an orphaned language, that has not been actively updated beyond system needs for a decade now. As shame too, because it could have been a powerful language for the EMR had it been continually developed. As a niche language, it would have been less prone to viral attacks and malicious code, a quality you want in your EMR, I should think. 😉
I suggest you create a new array that is one column larger than the original array.
Copy the original array into the new array, making the first element in each row the element you want to sort by. Then, just have at it.
Now, for extra credit, note that you can actually create a two-factor sort by concatenating the two elements you want to sort by! Be sure to pad the first elements with lots of spaces so that the resulting sort is accurate.