locked
Comparing Length property of 2 or more arrays to find the array with the most objects RRS feed

  • Question

  • Hello all,

    How can you use PowerShell to compare multiple arrays to find out which array contains the most objects? I'm guessing I can work with the .length property on an array but I can't figure out how to use compare-object with that Property. I may be way off base here.

    For example:

    $array1 = @(1,2,3,4,5)

    $array2 = @("cat","dog")

    The arrays contain different types of objects, but I want PowerShell to be able to tell me that array1 has the most objects (5 objects vs. 2 objects for array2)

    Thanks!


    Monday, May 19, 2014 9:13 PM

Answers

  • The Compare-Object cmdlet is going to compare what is in the arrays. It is going to report that array1 does not have cat and dog in it, and that array2 is missing the numeric values 1 through 5. You just need some simple logic to compare the lengths and report back.

    $array1 = @(1,2,3,4,5)
    $array2 = @("cat","dog")
    
    If ($array1.Length -gt $array2.Length) {
        Write-Output 'Array1 has more objects.'
    } ElseIf ($array2.Length -gt $array1.Length) {
        Write-Output 'Array2 has more objects.'
    } ElseIf ($array1.Length -eq $array2.Length) {
        Write-Output 'The arrays have the same number of objects.'
    } Else {
        Write-Output 'Unable to determine which array has more objects.'
    }
    Edit: Forgotten period.

    Monday, May 19, 2014 9:29 PM
  • Hmm, yes I had thought of this logic, however this method would be quite cumbersome if you had more than 2 arrays. I guess I was hoping there would be an easy way to compare multiple arrays to see which one is the largest. However, it doesn't appear there is. I'll go ahead and mark your response as answer, since it seems like the only way to accomplish this. :(
    • Marked as answer by KentYeabower Tuesday, May 20, 2014 12:49 PM
    Tuesday, May 20, 2014 12:49 PM

All replies

  • The Compare-Object cmdlet is going to compare what is in the arrays. It is going to report that array1 does not have cat and dog in it, and that array2 is missing the numeric values 1 through 5. You just need some simple logic to compare the lengths and report back.

    $array1 = @(1,2,3,4,5)
    $array2 = @("cat","dog")
    
    If ($array1.Length -gt $array2.Length) {
        Write-Output 'Array1 has more objects.'
    } ElseIf ($array2.Length -gt $array1.Length) {
        Write-Output 'Array2 has more objects.'
    } ElseIf ($array1.Length -eq $array2.Length) {
        Write-Output 'The arrays have the same number of objects.'
    } Else {
        Write-Output 'Unable to determine which array has more objects.'
    }
    Edit: Forgotten period.

    Monday, May 19, 2014 9:29 PM
  • Hmm, yes I had thought of this logic, however this method would be quite cumbersome if you had more than 2 arrays. I guess I was hoping there would be an easy way to compare multiple arrays to see which one is the largest. However, it doesn't appear there is. I'll go ahead and mark your response as answer, since it seems like the only way to accomplish this. :(
    • Marked as answer by KentYeabower Tuesday, May 20, 2014 12:49 PM
    Tuesday, May 20, 2014 12:49 PM