Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Group ArrayList of Custom Objects by Property

I would like to group $mya‘s objects based on bytes:

Name           Bytes
----           -----
.\filedata.py  {105, 109, 112, 111...}
.\testfile.txt {100, 102, 115, 97...}
.\copyfile.txt {100, 102, 115, 97...}

I’ve tried

$mya | Group-Object -Property Bytes

But the result I get is strange:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Count Name                      Group
----- ----                      -----
    3 {105, 109, 112, 111, 1... {@{Name=.\filedata.py; Bytes=System.Object[]}, @{Name=.\testfile.txt; Byte...

I don’t understand why I got this result. I would have thought that the pipeline unwrapped all objects from $mya and grouped them based on their requested property, Bytes.

My desired outcome is:

Count Name                        Group
----- ----                        -----
    1 {105, 109, 112, 111, ...}   {\.filedata.py}
    2 {100, 102, 115, 97, ...}    {\.testfile.txt, .\copyfile.txt}

>Solution :

Group-Object can’t quite figure out what to do with your byte array values.

Use an anonymous property expression to create something Group-Object can meaningfully compare – like strings – from the byte arrays:

$mya | Group-Object -Property { [BitConverter]::ToString($_.Bytes) }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading