Home > Blockchain >  Compare values from one array with the values from 2nd array and based on that place the values from
Compare values from one array with the values from 2nd array and based on that place the values from

Time:03-16

I have these 2 arrays. Values in Array 1 are in the correct order. Values in the Array 2 are in incorrect order. What I need to do is loop through both array and sort the IPs so that the same IPs are in the same index in the array 2.

  1. I was trying nested loops .

  2. I was trying the nested loops below but no luck.

  3. I have also tried nested ForEach but instead of getting the cross section, I get everything.

  4. I have checked the compare-object cmdlet but it does not really seem useful in this case.

  5. Any ideas?

$arrayOfItemsToCompare | ForEach-Object { if ($array -contains $) { Write-Host $ } }

Array 1:

192.168.3.1
192.168.1.1
10.49.50.254
0.0.0.0
90.183.197.126
194.228.115.69
90.182.79.231
90.182.78.193
194.228.115.67
194.228.115.66
194.228.190.115
194.228.92.85
172.253.50.253
108.170.238.231
216.58.201.68

Array 2:



DestinationIP   AverageLatency Success
    -------------   -------------- -------
    192.168.3.1                  0     100
    192.168.1.1                  0     100
    194.228.115.69            16.4     100
    90.183.197.126              17     100
    90.182.78.193             16.8     100
    194.228.115.67            16.8     100
    194.228.115.66            17.4     100
    194.228.190.115           17.8     100
    172.253.50.253            17.8     100
    194.228.92.85             18.4     100
    108.170.238.231             18     100
    216.58.201.68             17.6     100
    10.49.50.254                 0       0
    90.182.79.231                0       0

CodePudding user response:

One way you can sort your second array of objects based in the order of the first array could be by constructing an array of expressions.

Since this can be a bit hard to understand, let's use a simple example to understand this better. Given our example array 1, 2, 3 let's say we want to sort it in the order 3, 1, 2. For that we can use multiple expressions with Sort-Object, the expressions are script blocks that will determine where a condition was $true and sort the result based on that order:

PS /> 1, 2, 3 | Sort-Object { $_ -eq 3 }, { $_ -eq 1 }, { $_ -eq 2 } -Descending

3
1
2

Using the same logic, here is how your second array can be sorted. Below assumes that $sortArray is the variable that contains the list of IPs and $arrayOfObjects is the variable containing the array of objects we need to sort based on the order of the first variable:

# ASSUMING THE ARRAY OF IPs is stored here
$sortArray = @(...)

# Store all the expressions in a variable
$expressions = foreach($i in $sortArray) {
    { $_.DestinationIP -eq $i }.GetNewClosure()
}

$arrayOfObjects | Sort-Object $expressions -Descending

Another alternative could be using Group-Object -AsHashtable to create a lookup table based on the DestinationIP values of $arrayOfObjects and then loop through the $sortArray to filter the objects:

$map = $arrayOfObjects | Group-Object DestinationIP -AsHashTable
foreach($obj in $sortArray) {
    if($value = $map[$obj]) {
        $value
    }
}

The output from both snippets should look like below:

DestinationIP   AverageLatency Success
-------------   -------------- -------
192.168.3.1     0              100
192.168.1.1     0              100
10.49.50.254    0              0
90.183.197.126  17             100
194.228.115.69  16.4           100
90.182.79.231   0              0
90.182.78.193   16.8           100
194.228.115.67  16.8           100
194.228.115.66  17.4           100
194.228.190.115 17.8           100
194.228.92.85   18.4           100
172.253.50.253  17.8           100
108.170.238.231 18             100
216.58.201.68   17.6           100
  • Related