How to sort an array

Hi I need your help to sort an array by using this rules

start from 1-9 then add 0
but the input will be always changing
so output example will be like this:
So 1BO, 1OC, 2BO, 2OC, … 9BO, 9OC, 0BO, 0OC

thank you

@StevenIsRobotOnline

First, you need an array of strings that you want to sort.
Create a custom sorting function to define the sorting order. In your case, you want to sort the elements such that numbers 1-9 come before 0.

Example:

``````Function CustomSort(s1 As String, s2 As String) As Integer
Dim num1 As Integer = Integer.Parse(s1.Substring(0, 1))
Dim num2 As Integer = Integer.Parse(s2.Substring(0, 1))

If num1 = 0 Then num1 = 10
If num2 = 0 Then num2 = 10

Return num1.CompareTo(num2)
End Function
``````

Use the `Array.Sort` method along with the custom sorting function to sort the array.

`Array.Sort(yourArray, New Comparison(Of String)(AddressOf CustomSort))`

This code will sort the `yourArray` based on your custom sorting rule and output the sorted array.

``````Dim yourArray As String() = {"1BO", "0OC", "2BO", "9OC", "3BO", "0OC", "8BO", "4OC", "0BO", "7OC", "6BO", "5OC"}

Function CustomSort(s1 As String, s2 As String) As Integer
Dim num1 As Integer = Integer.Parse(s1.Substring(0, 1))
Dim num2 As Integer = Integer.Parse(s2.Substring(0, 1))

If num1 = 0 Then num1 = 10
If num2 = 0 Then num2 = 10

Return num1.CompareTo(num2)
End Function

For Each item In yourArray
LogMessage(item) ' Output the sorted array
``````

Next

Try this

``````Enumerable.Range(1, 9).Concat(New Integer() {0}).ToArray()

(From n In Array
From s In {"BO", "OC"}
Select n.ToString() & s).ToArray()

String.Join(",",Pattern)
``````

Hope this helps!!

Hi, i want to add the letter BO SO etc is random too

The Mention was to sort an already existing array in your initial post, So is an Array available with your or is the Post about creating the Array with the set pattern ?

Hello @StevenIsRobotOnline

Assign
ArrayVariable = {“2OC”, “0OC”, “1BO”, “3OC”, “0BO”, “1OC”, “4BO”}

Assign
SortedArray = ArrayVariable.OrderBy(Function(x) CustomSort(x)).ToArray()

Thanks & Cheers!!!

the array i receive will be dynamic so i will need to sort it out based on the value i got ,

the example of array i will receive is like this So 1BO, 1OC, 2BO, 2OC, … 9BO, 9OC, 0BO, 0OC

but it will keep changing every process run

Could you let us know what is the sorted version of the example array provided according to you ?

To sort an array in UiPath using the rules you provided, you can use the following steps:

1. Create a new variable of type `Array<String>` to store the sorted array.
2. Iterate through the original array and add each element to the sorted array,using the following rules:
• If the element starts with a number, add it to the sorted array in ascending order.
• If the element starts with a letter,add it to the sorted array in alphabetical order.
1. Return the sorted array.

Here is a UiPath workflow that you can use to sort an array using the rules you provided:

// Create a new variable to store the sorted array Array sortedArray = New Array()

// Iterate through the original array and add each element to the sorted array, using the following rules: foreach (String element in originalArray) { // If the element starts with a number, add it to the sorted array in ascending order. If (element.StartsWith(Regex.Match(element, “[1]”).Value)) Then { sortedArray.Add(element, ascending: True) } // If the element starts with a letter, add it to the sorted array in alphabetical order. ElseIf (element.StartsWith(Regex.Match(element, “[2]”).Value)) Then { sortedArray.Add(element, ascending: True) } End If }

// Return the sorted array Return sortedArray

To use this workflow, you would need to pass in the original array as a parameter to the workflow. The workflow would then return the sorted array.

Here is an example of how to use the workflow:

``````// Get the original array
String[] originalArray = New String[] { "1BO", "1OC", "2BO", "2OC", ... "9BO", "9OC", "0BO", "0OC" }
``````

// Sort the original array using the rules you provided
String sortedArray = SortArray(originalArray)

// Print the sorted array
foreach (String element in sortedArray)
{
Console.WriteLine(element)
}`

Output:

`0BO 0OC 1BO 1OC 2BO 2OC ... 9BO 9OC`

Cheers @StevenIsRobotOnline

1. 0-9 ↩︎

2. a-zA-Z ↩︎

@supermanPunch

thank you

with the restricition, that 0 is after 9 we cannot order numerical
Also we cannot sort lexically

So we would split onf first digit, then second sort on Letter

arrCustomNoSort | StringArray =

``````Enumerable.Range(1,9).Append(0).Select(Function (x) x.ToString).toArray
``````

arrSorted | String Array =

``````(From x In arrUnsorted
Let m = System.Text.RegularExpressions.Regex.Match(x, "^(\d)(.*)")
Let d = arrCustomNoSort.ToList.FindIndex(Function (s) m.Groups(1).Value.Equals(s))
Let l = m.Groups(2).Value
Order By d,l
Select v =x).ToArray
``````

Hi PRR , i see your solution almost perfect,
1 more condition maybe i need to mention is it will need to first sort all the similar letters ,
so like all BO first then AO etc

output :
1BO,5BO,9BO,1OC,2OC,3OC,00C,—
or
1BO,5BO,9BO,0BO,1OC,2OC,3OC,OOC,—

give a try at swapping the order prios

arrSorted | String Array =

``````(From x In arrUnsorted
Let m = System.Text.RegularExpressions.Regex.Match(x, "^(\d)(.*)")
Let d = arrCustomNoSort.ToList.FindIndex(Function (s) m.Groups(1).Value.Equals(s))
Let l = m.Groups(2).Value
Order By l,d
Select v =x).ToArray
``````

we changed
d,l → l,d