# get-sortedlist.ps1
# Sortedlist sample using PowerShell
# Thomas Lee - tfl@psp.co.uk
# Define helper function
function PrintKeysAndValues( $myList ) {
"`t-KEY-`t-VALUE-"
for ( [int] $i = 0; $i -lt $myList.Count; $i++ ) {
"`t{0}:`t{1}" -F $myList.GetKey($i), $myList.GetByIndex($i)
}
""
}
# Create and initialise a new SortedList object
$mySL = new-object system.collections.SortedList
$mySL.Add("First", "Hello")
$mySL.Add("Second", "World")
$mySL.Add("Third", "!")
# Display the properties and values of the SortedList
"`$mySL"
" Count: {0}" -f $mySL.Count
" Capacity: {0}" -f $mySL.Capacity
" Keys and Values:"
PrintKeysAndValues( $mySL )
# Add two more and display results
$mysl.add("aaaa", "aaaa")
$mysl.add("zzzz" , "zzzz")
# display results
"`$mySL after two additions"
" Count: {0}" -f $mySL.Count
" Capacity: {0}" -f $mySL.Capacity
" Keys and Values:"
PrintKeysAndValues( $mySL )
This script produces the following output
PS D:\foo>
D:\foo\get-sortedlist.ps1
$mySL
Count: 3
Capacity: 16
Keys and Values:
-KEY- -VALUE-
First: Hello
Second: World
Third: !
$mySL after two additions
Count: 5
Capacity: 16
Keys and Values:
-KEY- -VALUE-
aaaa: aaaa
First: Hello
Second: World
Third: !
zzzz: zzzz