I would like to binarysort a generic list of pscustomobject, but I don't know how to write code for it.
Do anyone know how to write the code for binarysearching a pscustomobject list?
array of pscustomobject
$list1 =
[pscustomobject]@{Id = 5; Name = "Tom"},
[pscustomobject]@{Id = 1 ; Name = "Ben"},
[pscustomobject]@{Id = 8 ; Name = "Alice"},
[pscustomobject]@{Id = 4 ; Name = "Lucy"},
[pscustomobject]@{Id = 9 ; Name = "Sam"},
[pscustomobject]@{Id = 7 ; Name = "Bill"}
Generic List of pscustomobject
using namespace System.Collections.Generic;
$l1 = [List[PSCustomObject]]::new()
$list1 | % {$l1.add($_)}
Sort the list
$l1.Sort( { $args[0].name.compareto($args[1].name) })
The follwing code doesn't work...
PS> $l1.BinarySearch({$args.Name -eq "Ben"})
Error
MethodInvocationException: Exception calling "BinarySearch" with "1" argument(s): "Failed to compare two elements in the array."