I have been using [Linq.Enumerable]::SequenceEqual()
to compare the order of items in two arrays, for example
$validOrder = @('one', 'two', 'three')
$provided = @('one', 'two', 'three')
[Linq.Enumerable]::SequenceEqual($validOrder, $provided)
This works but now I realize I want to address capitalization errors independently, so I want to test for order in a case insensitive way.
I found this that documents a different method signature, with IEqualityComparer<T>
as the third value. That sure seems like the right direction, but I haven't found anything that helps me implement this in powershell. I tried just using 'OrdinalIgnoreCase' for the last argument, which works for [String].FindIndex()
as was pointed out in another thread. But alas not here. I also found this which actually makes a custom comparer for a different object type, but that seems like I am just then implementing manually the thing that I actually want, and I am not sure what the value would be of using [Linq.Enumerable]::SequenceEqual()
, I could just pass my arrays to my class method and do the work there directly.
I have also made this approach work if (-not (Compare-Object -SyncWindow 0 $validOrder $provided)) { $result = 'ordered' } else { $result = 'disordered' } And it is already case insensitive. But it is also slower, and I may have a lot of these tests to do, so speed will be of benefit.
Lastly I see that this seems to work, and is SUPER simple, case insensitive (if I want) and seemingly fast. The total number of items in the array will always be small, it is the number of repeats with different array that is the performance issue.
$result = ($provided -join ' ') -eq ($validOrder -join ' ')
So, is this last option viable, or am I missing something obvious that argues against it?
Also, I feel like I will have other situations where IEqualityComparer<T>
is a useful argument, so knowing how to do it would be useful. Assuming I am reading this right and IEqualityComparer<T>
provides a mechanism for a different form of comparison, other than just rolling my own comparison.