Question With Custom Sorting A Dictionary

Hi UiPath Forum,

I had a question regarding developing a custom sorting function for a dictionary object. I see that the SortedDictionary object has a constructor that takes a IComparer object as a constructor. I want to utilize this constructor to custom sort my created dictionary, however, I am not 100% sure how to implement this in UiPath. Would anyone mind pointing me in the right direction? I would attach my sample project with what I have tried (and hasn’t worked) so far but I am not allowed to add attachments as I am a new user, so I have added screenshots below.

image

Assign Activity:

New SortedDictionary(Of String, Dictionary(Of String, String)) From {
	{"Second",  
			New Dictionary(Of String, String) From {
				{"Hello", "Second"}
			}
	},
	{"First",
	    New Dictionary(Of String, String) From {
				{"Hello", "First"}
		}
	},
	{"Third",
	  New Dictionary(Of String, String) From {
	    {"Hello", "First"}
	  }
	}
}

Invoke Code Block Here

Public Class DicComp
    Inherits IComparer(Of String)

    Public Function Compare(ByVal key1 As String, ByVal key2 As String) As Integer
        console.WriteLine("key1: " & key1)
        console.WriteLine("key2: " & key2)

        If key1.Length < key2.Length Then
            Return 1
        ElseIf key1.Length > key2.Length Then
            Return -1
        Else
            Return 0
        End If
    End Function
End Class

Thank you to anyone who reads/responds to this,
Angel

Maybe WE can do dort IT with the Help of LINQ

Lets asume WE Sort the Nested Dict on Outer Key length

(From KVP in Yournesteddictvar
Order by KVP.key.length
Select k=KVP).toDictionary(function (x) x.Key, function (k) k.value)

Sorry, I didn’t mean to say I want to sort by key length. That was just an example I had. I essentially want to sort by the Key Name.

For example,

I want the below dictionary to be sorted by “First”, “Second”, “Third”.

New SortedDictionary(Of String, Dictionary(Of String, String)) From {
	{"Second",  
			New Dictionary(Of String, String) From {
				{"Hello", "Second"}
			}
	},
	{"First",
	    New Dictionary(Of String, String) From {
				{"Hello", "First"}
		}
	},
	{"Third",
	  New Dictionary(Of String, String) From {
	    {"Hello", "First"}
	  }
	}
}

I’m confident I can figure the logic out. I’m having more trouble on actually creating the Comparer variable and initializing it with my own sorting logic so I can then call the below constructor to sort the dictionary for me.

To make the custom class ( in your Case the compairer)available IT is common to Provide the implementation by a nuget package and add IT to the Project via dependency manager

Calling the constructor give a try on

New SortedDictionary(Of String, Dictionary(Of String, String))( New DicComp()) From …

The exact Sort criteria / Evaluation still ist Not enough clear derivable from your Posts.

We Setup custom Sports eg by an Array / Dict… And use the Item Position Index for the sorting.

So decide If you want to Focus on compairer and its Provision or on custom sorting

For Last have a Look Here

https://forum.uipath.com/search?context=topic&context_id=325802&q=Custom%20Sort%20@ppr&skip_context=true

And Here

Thank you Peter. This is helpful.

To give an idea on the sorting I need, consider the below:

I have a dictionary that has the following:

Dictionary(Of String, String) {
  {"Catwoman", "..."},
  {"Joker", "..."},
  {"Superman", "..."},
  {"Batman", "..."},
  {"Riddler", "..."}
}

Let’s say the below strings take priority based on the given order:

  1. Superman
  2. Batman
  3. Catwoman
  4. Joker
  5. Riddler

Based on the above order, I want to sort the Dictionary to make it look like below:

Dictionary(Of String, String) {
  {"Superman", "..."},
  {"Batman", "..."},
  {"Catwoman", "..."},
  {"Joker", "..."},
  {"Riddler", "..."}
}

I am going through the post you shared and trying to see if I can translate it over to my use-case.

Thank you once again,