Change order of keys in dictionary?

I have a transaction item (dictionary) with keys of, say…Name, Age, and City.

While processing the item, I add keys with Date and Result. Then I want to include Name, Age, City, Date, and Result in a confirmation email - but I want Date and Result first, not last.

Is there a way to change the order of the keys within a dictionary? Or if there’s a way to have it add a new key at the beginning instead of the end, that would work too.

you can use OrderedDictionary class System.Collections.Specialized.OrderedDictionary

myOrderedDictionary = new system.Collections.Specialized.OrderedDictionary()
myOrderedDictionary.Insert(0, “insertedKey1”, “insertedValue1”)

That’s an interesting thing, but I don’t know if it helps me here because the dictionary is actually transactionItem.SpecificContent which is automatically created by the Get Transaction activity. I don’t think I can control the datatype of SpecificContent.

I suppose I could create a transactionDict as an OrderedDictionary, and copy SpecificContent to it then work with transactionDict in the rest of my automation. Might be a reasonable solution if nothing better is available.

Or you can store an array with the order you want as {“Date”, “Result”, “Name”, “Age”, “City”}

then,

for each item in {“Date”, “Result”, “Name”, “Age”, “City”}:
write log: item.tostring + “:” +dic(item).tostring

I’m not sure what your email output is like but something like this can be done for ordering.

@postwick

  • in general it is more about the access order when the dictionary is used for the email.
  • we can use the prepend method for adding an item on the first position

However with following approach it can be reordered and will not loose items which are not defined in the order definition:

Variable:
grafik

input:

flow:
grafik

(From kvp In dictDemo
Order By Array.IndexOf(arrOrder, kvp.Key) Descending).ToDictionary(Function (x) x.Key,Function (x) x.Value)

Result:
grafik

Kindly note: as we do reverse the order to handle -1 from indexof for not specified keys in the sort order the first element has to be the last item within the sort definition array

HAHA this worked like a charm. I partially understand how/why it works, but I never would have figured that out on my own.

THANK YOU

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.