[UiPath] Sort Dictionary by Key/Value (with example)

Dictionaries are special data types that store pairs of some values.
Each pair consist of Key and Value.

Basic ways how to work with Dictionaries are covered in our RPA Foundation course.

So let’s do just a quick recap.

:arrow_down_small: You can download example file for this article here: SortDictionary.zip (36.5 KB) :arrow_down_small:

Initialization and adding values

  1. Create a variable and Browse for types, look for:
    System.Collections.Generic.Dictionary<TKey, TValue>.
    Pick your desired data type of key and value.
    obrazek

  2. Initialize default value of empty dictionary with:
    New Dictionary(Of String, Int32)
    obrazek

  3. Add items using Assign activity. Write key on the left side and value on the right side as this:
    obrazek
    Where

  • key = Isaac Asimov
  • value = 1920

Sort Dictionary

You can create a new, sorted Dictionary from an unsorted one using this VisualBasic .NET method within the Assign activity.

unsorted_dictionary.OrderBy(function(x) x.Value).ToDictionary(function(y) y.Key,function(y) y.Value)

obrazek

In our example, ( SortDictionary.zip (36.5 KB) ) you can see both ways to sort: by key and also by value.

:love_you_gesture:If you would like to sort in descending order, change the method from OrderByOrderByDescending

3 Likes