Hi there,
I am currently doing the RPA Developer Foundation course. My question is based on the data manipulation module, practice 1 - Lists. Please find my question at the bottom of the page, below is information on where my question originates from.
This is the task:
Sort the List and Print 3 Values
Given an input list of countries please sort the list and print the first 3 values in descending order.
Note: Please use this value for the List initialization: new List(of String) from {“Germany”, “Spain”, “Japan”, “Brazil”, “India”, “China”}.
This is the solution:
- Start the project as a sequence and define a L ist of Strings variable (“InputData”) with the following default value: new List(of String) from {“Germany”, “Spain”, “Japan”, “Brazil”, “India”, “China”}.
- Add an ’ Invoke Method ’ activity. Write ’ Sort ’ under MethodName and “InputData” under TargetObject. Leave the TargetType unchanged (null).
- Add one more ’ Invoke Method ’ activity. Write ’ Reverse ’ under MethodName and have the other 2 fields filled in just like in the previous ‘Invoke Method’
- Create a new List of String variable.
- Add an ‘Assign’ activity to the Designer panel.
- In the To field, enter the variable created at step 4.
- In the Value field, enter the name of the original variable, followed by .GetRange(0,3). This variable stores the first three values in the sorted list.
- Add a ‘Write Line’ activity. This will print the values.
- Use the ‘String.Join’ method: String.Join (", ", secondvariablename .ToArray) in the text of the ‘Write Line’ activity.
My question:
For the last step (step 9.), I don’t understand why we have to convert the secondvariablename which is the new list we created into an array in order to print the 3 items. When we used the assign activity and do secondvariablename = firstvariablename.GetRange(0,3), I thought we already populated secondvariablename with the three items and stored them as a list. Why do we have to use the .ToArray method, why doesn’t the String.Join method work on the secondvariablename which is already a list?
Thanks in advance for answering my question.