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.
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
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