Using Add-Member
I added a tiny method to an array of objects to calculate sort of a hash from a string:
$arr | Add-Member -MemberType ScriptMethod -Name 'SortedName' -Value {
[string]$name = $this.Name
[int]$start = [int][char]$name[0];
[int]$index = [int]($name -replace '^.+?(\d+)\.[^.]+$','$1')
[int]"$start$('{0:D4}' -f $index)"
}
Now I'd like to sort the array using $arr | Sort-Object -PropertyName 'SortedName'
. But that doesn't seem to work as expected.
How can I apply Sort-Object
on an array's methods?