0

I am comparing .JSON files, using Compare-Object like this:

# Compare two files 
$BLF = (Get-Content -Path C:\Users\...\Documents\Android1.json)
$CLF = (Get-Content -Path C:\Users\...\Documents\Android2.json)

$a = Compare-Object -ReferenceObject $BLF -DifferenceObject $CLF -IncludeEqual 

Then $a is a System.Array. It contains all the lines in the .JSON files each in an InputObject, and SideIndicator to show the difference. Thus for a difference such as

# From file 1  
"workProfilePasswordMinimumLength":  false,
"workProfilePasswordMinLowerCaseCharacters":  false,
"workProfilePasswordMinUpperCaseCharacters":  null,
"deviceManagementApplicabilityRuleOsEdition":  null,

# From file 2 
"workProfilePasswordMinimumLength":  true,
"workProfilePasswordMinLowerCaseCharacters":  false,
"workProfilePasswordMinUpperCaseCharacters":  null,
"deviceManagementApplicabilityRuleOsEdition":  true,

the compare function gives using

$a.psobject.ImmediateBaseObject

the output:

InputObject                                              SideIndicator
-----------                                              -------------
    "workProfilePasswordMinLowerCaseCharacters":  false, ==           
    "workProfilePasswordMinUpperCaseCharacters":  null,  ==           
    "workProfilePasswordMinimumLength":  true,              =>           
    "deviceManagementApplicabilityRuleOsEdition":  true, =>           
    "workProfilePasswordMinimumLength":  false,             <=           
    "deviceManagementApplicabilityRuleOsEdition":  null, <=    

My probem is: I want to sort these InputObjects so that they are like this:

InputObject                                              SideIndicator
-----------                                              -------------
    "workProfilePasswordMinLowerCaseCharacters":  false, ==           
    "workProfilePasswordMinUpperCaseCharacters":  null,  ==           
    "workProfilePasswordMinimumLength":  true,              =>           
    "workProfilePasswordMinimumLength":  false,             <=     
    "deviceManagementApplicabilityRuleOsEdition":  true, =>     
    "deviceManagementApplicabilityRuleOsEdition":  null, <=     
  

So that the matching names, when the variables are different, are sorted. How can I sort them?

esr
  • 1

1 Answers1

1

You can pipe the output of the Compare-Object cmdlet to Sort-Object like so

Compare-Object -ReferenceObject $BLF -DifferenceObject $CLF -IncludeEqual |
    Sort-Object -Property InputObject

Since it sorts the output by InputProperty, you get

InputObject                                         SideIndicator   
-----------                                         -------------   
"deviceManagementApplicabilityRuleOsEdition":  null <=
"deviceManagementApplicabilityRuleOsEdition":  true =>
"workProfilePasswordMinimumLength":  false          <=
"workProfilePasswordMinimumLength":  true           =>
"workProfilePasswordMinLowerCaseCharacters":  false ==
"workProfilePasswordMinUpperCaseCharacters":  null  ==

This is slightly different from the output you posted. If you need the output exactly as you posted, then you can use a custom sorting expression.

Sort-Object -Property @{Expression={ <some expression> }}

Clijsters
  • 4,031
  • 1
  • 27
  • 37
Sarin
  • 1,255
  • 11
  • 14