System.Collection.Arraylists containing objects

by Lembasts at 2013-02-13 15:14:49

Greetings,
Having been persuaded to use the system.collections.arraylist class instead of boring old arrays, I had a look at the methods and was wondering how to use some of these when your arraylist contains objects. For example, how do I sort on a property of the objects? How do I search on a property? how can I remove an array element without specifying an index? Or do arraylists containing objects not allow you to use certain methods?
by teezee at 2013-02-13 17:52:20
I will admit that I’m very new to all this and I don’t want you to take this as an awnser simply because I couldn’t really understand the question very well and I don’t have much programmation background… all in all…

This is what I came with trying to figure out if that is what you’re talking about… :smiley:


#setup arraylist
$arraylist = New-Object System.Collections.ArrayList
#get data
$list1 = Get-ChildItem -Directory -Recurse -Path "h:"
#get more data woohoo
$list2 = Get-ChildItem -Directory -Recurse -Path "d:"
#add object to list
$arraylist.Add($list1)
#add more vodka to the juice
$arraylist.Add($list2)

#list only the fullname of all the folders in $list2
$arraylist[1].fullname

#list only the second folder of $list2
$arraylist[1][1].fullname

#delete the fourt folder of $list1 recursively with all its files
#$arraylist[0][3].delete($true) – don’t do this at home… :wink:
#to search you can do some foreach…

#now I believe you see that there is alot more you can do from here.



Well that is what I understood :X
by Lembasts at 2013-02-13 18:44:04
I can easily access the objects in the arraylist and the properties…its more about sorting on specific properties, searching on specific properties and using the remove without using an index.
by mjolinor at 2013-02-14 05:25:57
Here’s an example of removing entries without using the index:

$al = new-object collections.arraylist

gci c:\testfiles*.xml |
select name,length |
foreach {
$al.add($) > $null
}
’*‘80
$al
’*80
foreach ($entry in ($al | where {$
.length -gt 1mb} ))
{$al.remove($entry)}

$al
by Lembasts at 2013-02-14 13:04:55
Very nice solution - thanks.
So can we do sorting and searching on specific object properties?
by Lembasts at 2013-02-14 13:38:07
Just did a comparison and it turns out that using indexes for deletion:

$mastermax = $master.count
for ($masteridx = 0;$masteridx -lt $mastermax;$masteridx++) {
if ($master[$masteridx].DateAD -eq “removed” -and
$master[$masteridx].Client -eq “no” -and
(Get-Date $master[$masteridx].lastlogontimestamp) -lt $oldtime -and
(Get-Date $master[$masteridx].PasswordLastSet) -lt $oldtime ) {
$master.removeat($masteridx)
$sumdel++
$masteridx–
$mastermax–
}
}


is around 3 to 4 times quicker than using objects:

$mastermax = $master.count

foreach ($entry in ($master | where { $.DateAD -eq “removed” -and
$
.Client -eq “no” -and
(Get-Date $.lastlogontimestamp) -lt $oldtime -and
(get-date $
.PasswordLastSet) -lt $oldtime } )) {
$master.remove($entry)
$sumdel++
}