I have an array of strings make like this "name: number" and I need to order it by number every time that I add another row to the array but the sorting doesn't work.
The idea is that every time that the function is called I create a new row and I add it to the text. Then, for each row in the text I add in front of the row the number and a '@'. Now the array is like this:
15@foo:15
2@bar:2
4@foobar:4
Now I'd like to order it by number and then remove the part that I added to sort.
Function Add($name, $number) {
$newRow = "$($name):$number`r`n"
$Script:text = $Script:text + $newRow
ForEach($row in $text.Split("`r`n")) {
if ($row.length -ne 0) {
$rows = $rows + $row.Split(":")[1] + "@" + $row + "`r`n"
}
}
$rows | Sort-Object
ForEach($row in $rows.Split("`r`n")) {
$newText = $newText + $row.Split("@")[1] + "`r`n"
}
$textBox.Text = $newText
}
It all works except the sorting.
Does anyone knows how to fix it?
$rows | Sort-Objectoutputs the results of the sort as a new array -Sort-Objectnever sorts in place; therefore:$rows = $rows | Sort-Object