Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Sort an array of numbers in Powershell

Reply
 
Old 12-17-2008   #1 (permalink)
stephenodonoghue


 
 

Sort an array of numbers in Powershell

Does anyone know a way to sort an array of numbers in Powershell?

My System SpecsSystem Spec
Old 12-17-2008   #2 (permalink)
stephenodonoghue


 
 

Re: Sort an array of numbers in Powershell

Hi Stuart,

Thanks for replying. Basically what I'm trying to do is scrape a list
of prices from a list of eBay auctions and sort them in descending
order. With the code I'm using, everything is sorted into an array
list. I don't have much experience with these kinds of Collections, so
I'm not exactly sure how to sort things things. I thought there might
just be a Sort() method you could call. But that turns out not to be
the case from what I can see.

I tried using your example. On its own it works fine for me. I wasn't
quite able to integrate it with my code and get to sort everything in
order. I would probably have chuck away some of the existing code and
start again to get it to work.

Here is the code I'm using:
param ([string]$url = $(throw 'Enter a url'))

function ScrapePrices([String] $webpageText)
{
$returnMatches = New-Object System.Collections.ArrayList
$regex = @"
<td class=\"prices g-b\">(\$|€£)([0-9]{1,2}\.[0-9]{1,2})\</td>
"@
$resultingMatches = [Regex]::Matches($webpageText.ToString(), $regex,
"IgnoreCase")
foreach($match in $resultingMatches)
{
$cleanedMatch = $match.Groups[2].Value.Trim()
[void] $returnMatches.Add($cleanedMatch)
}
$returnMatches
}
function DisplayPrices([System.Collections.ArrayList]$list)
{
##foreach ($item in ($list.getEnumerator() | Sort Key -desc) )
{ Write-Host $item } #doesn't work
}
function GetWebpage([String]$url)
{
[System.Net.HttpWebRequest] $request =
[System.Net.HttpWebRequest]::Create($url)
$request.Timeout = 300

[IAsyncResult] $asyncResult = $request.BeginGetResponse($null, $null)

[System.Net.WebResponse] $response = $request.EndGetResponse
($asyncResult)
[System.IO.StreamReader] $respStreamReader = new-object
System.IO.StreamReader $response.GetResponseStream()
[string] $responseHTML = $respStreamReader.ReadToEnd()

$respStreamReader.Close()
$response.Close()

trap [System.Exception]
{
write-host "There was an error while trying to connect to `"$url`""
`n
Write-Host "Exception: " System.Exception
}
return $responseHTML
}
Clear-Host

DisplayPrices( (ScrapePrices (GetWebpage $url)) )


..\myScript.ps1 "http://books.shop.ebay.com/items/Audiobooks__The-Da-
Vinci-Code_W0QQLength87732706ZUnabridged36be1033QQ
Condition441b8cfbZBrandNew486d0d47QQ_catrefZ1QQ_dmptZUSQ5fAudiobooksQQ_flnZ1QQ_sacatZ29792QQ_ssovZ1QQ_trksidZp3286Q2ec0Q
2em282"
My System SpecsSystem Spec
Old 12-17-2008   #3 (permalink)
Kryten


 
 

Re: Sort an array of numbers in Powershell

Hi Stephen,

This works for me:-

$a = New-Object -comObject System.Collections.ArrayList
[void]$a.Add(98)
[void]$a.Add(3)
[void]$a.Add(56)
[void]$a.Add(18)
"Current Values in a are :`n$a"
$a.Sort()
"Sorted values in a are :`n$a"

When I pipe $a to get-member I don't see a sort() method either, but
when I put
a comma before $a like:-

,$a | gm

There's more there. Something to do with Uuuummmmmm.......I think
Keith Hill once explained it here, but for the life of me I can't
remember the details, sorry.

Did you try changing the last $returnmatches to $returnmatches.sort()
at the end of your
ScrapePrices function?

Cheers,
Stuart










On 17 Dec, 21:57, stephenodonog...@xxxxxx wrote:
Quote:

> Hi Stuart,
>
> Thanks for replying. Basically what I'm trying to do is scrape a list
> of prices from a list of eBay auctions and sort them in descending
> order. With the code I'm using, everything is sorted into an array
> list. I don't have much experience with these kinds of Collections, so
> I'm not exactly sure how to sort things things. I thought there might
> just be a Sort() method you could call. But that turns out not to be
> the case from what I can see.
>
> I tried using your example. On its own it works fine for me. I wasn't
> quite able to integrate it with my code and get to sort everything in
> order. I would probably have chuck away some of the existing code and
> start again to get it to work.
>
> Here is the code I'm using:
> param ([string]$url = $(throw 'Enter a url'))
>
> function ScrapePrices([String] $webpageText)
> {
> * * * * $returnMatches = New-Object System.Collections.ArrayList
> * * * * $regex = *@"
> <td class=\"prices g-b\">(\$|€£)([0-9]{1,2}\.[0-9]{1,2})\</td>
> "@
> * * * * $resultingMatches = [Regex]::Matches($webpageText.ToString(), $regex,
> "IgnoreCase")
> * * * * foreach($match in $resultingMatches)
> * * * * {
> * * * * * * * * $cleanedMatch = $match.Groups[2].Value.Trim()
> * * * * * * * * [void] $returnMatches.Add($cleanedMatch)
> * * * * }
> * * * * $returnMatches}
>
> function DisplayPrices([System.Collections.ArrayList]$list)
> {
> * * * * ##foreach ($item in ($list.getEnumerator() | Sort Key -desc) )
> { Write-Host $item } * * #doesn't work}
>
> function GetWebpage([String]$url)
> {
> * * * * [System.Net.HttpWebRequest] $request =
> [System.Net.HttpWebRequest]::Create($url)
> * * * * $request.Timeout = 300
>
> * * * * [IAsyncResult] $asyncResult = $request.BeginGetResponse($null, $null)
>
> * * * * [System.Net.WebResponse] $response = $request.EndGetResponse
> ($asyncResult)
> * * * * [System.IO.StreamReader] $respStreamReader = new-object
> System.IO.StreamReader $response.GetResponseStream()
> * * * * [string] $responseHTML = $respStreamReader.ReadToEnd()
>
> * * * * $respStreamReader.Close()
> * * * * $response.Close()
>
> * * * * trap [System.Exception]
> * * * * {
> * * * * * * * * write-host "There was an error while trying to connect to `"$url`""
> `n
> * * * * * * * * Write-Host "Exception: " System.Exception
> * * * * }
> * * * * return $responseHTML}
>
> Clear-Host
>
> DisplayPrices( (ScrapePrices (GetWebpage $url)) )
>
> .\myScript.ps1 "http://books.shop.ebay.com/items/Audiobooks__The-Da-
> Vinci-Code_W0QQLength87732706ZUnabridged36be1033QQ
> Condition441b8cfbZBrandNew486d0d47QQ_catrefZ1QQ_dmptZUSQ5fAudiobooksQQ_flnZ1QQ_sacatZ29792QQ_ssovZ1QQ_trksidZp3286Q2ec0Q
> 2em282"
My System SpecsSystem Spec
Old 12-17-2008   #4 (permalink)
stephenodonoghue


 
 

Re: Sort an array of numbers in Powershell

I actually managed to get it to work by modifying the function below
to something resembling the first example you gave me.


function DisplayListItems([System.Collections.ArrayList]$list)
{
$list | sort -desc -InputObject {[decimal]$_}
}

So that's basically it. Nice one. Cheers for your help.
My System SpecsSystem Spec
Old 12-17-2008   #5 (permalink)
Kryten


 
 

Re: Sort an array of numbers in Powershell

There's more there. Something to do with Uuuummmmmm.......I think
Quote:

> Keith Hill once explained it here, but for the life of me I can't
> remember the details, sorry.
No, it was Oisin!

http://groups.google.co.uk/group/mic...989da413364e12

Cheers,
Stuart


On 17 Dec, 22:57, Kryten <Kryte...@xxxxxx> wrote:
Quote:

> Hi Stephen,
>
> This works for me:-
>
> $a = New-Object -comObject System.Collections.ArrayList
> [void]$a.Add(98)
> [void]$a.Add(3)
> [void]$a.Add(56)
> [void]$a.Add(18)
> "Current Values in a are :`n$a"
> $a.Sort()
> "Sorted values in a are :`n$a"
>
> When I pipe $a to get-member I don't see a sort() method either, but
> when I put
> a comma before $a like:-
>
> ,$a | gm
>
> There's more there. Something to do with Uuuummmmmm.......I think
> Keith Hill once explained it here, but for the life of me I can't
> remember the details, sorry.
>
> Did you try changing the last $returnmatches to $returnmatches.sort()
> at the end of your
> ScrapePrices function?
>
> Cheers,
> Stuart
>
> On 17 Dec, 21:57, stephenodonog...@xxxxxx wrote:
>
Quote:

> > Hi Stuart,
>
Quote:

> > Thanks for replying. Basically what I'm trying to do is scrape a list
> > of prices from a list of eBay auctions and sort them in descending
> > order. With the code I'm using, everything is sorted into an array
> > list. I don't have much experience with these kinds of Collections, so
> > I'm not exactly sure how to sort things things. I thought there might
> > just be a Sort() method you could call. But that turns out not to be
> > the case from what I can see.
>
Quote:

> > I tried using your example. On its own it works fine for me. I wasn't
> > quite able to integrate it with my code and get to sort everything in
> > order. I would probably have chuck away some of the existing code and
> > start again to get it to work.
>
Quote:

> > Here is the code I'm using:
> > param ([string]$url = $(throw 'Enter a url'))
>
Quote:

> > function ScrapePrices([String] $webpageText)
> > {
> > * * * * $returnMatches = New-Object System.Collections.ArrayList
> > * * * * $regex = *@"
> > <td class=\"prices g-b\">(\$|€£)([0-9]{1,2}\.[0-9]{1,2})\</td>
> > "@
> > * * * * $resultingMatches = [Regex]::Matches($webpageText.ToString(), $regex,
> > "IgnoreCase")
> > * * * * foreach($match in $resultingMatches)
> > * * * * {
> > * * * * * * * * $cleanedMatch = $match.Groups[2].Value.Trim()
> > * * * * * * * * [void] $returnMatches.Add($cleanedMatch)
> > * * * * }
> > * * * * $returnMatches}
>
Quote:

> > function DisplayPrices([System.Collections.ArrayList]$list)
> > {
> > * * * * ##foreach ($item in ($list.getEnumerator() | Sort Key -desc) )
> > { Write-Host $item } * * #doesn't work}
>
Quote:

> > function GetWebpage([String]$url)
> > {
> > * * * * [System.Net.HttpWebRequest] $request =
> > [System.Net.HttpWebRequest]::Create($url)
> > * * * * $request.Timeout = 300
>
Quote:

> > * * * * [IAsyncResult] $asyncResult = $request.BeginGetResponse($null, $null)
>
Quote:

> > * * * * [System.Net.WebResponse] $response = $request.EndGetResponse
> > ($asyncResult)
> > * * * * [System.IO.StreamReader] $respStreamReader = new-object
> > System.IO.StreamReader $response.GetResponseStream()
> > * * * * [string] $responseHTML = $respStreamReader.ReadToEnd()
>
Quote:

> > * * * * $respStreamReader.Close()
> > * * * * $response.Close()
>
Quote:

> > * * * * trap [System.Exception]
> > * * * * {
> > * * * * * * * * write-host "There was an error while trying to connect to `"$url`""
> > `n
> > * * * * * * * * Write-Host "Exception: " System.Exception
> > * * * * }
> > * * * * return $responseHTML}
>
Quote:

> > Clear-Host
>
Quote:

> > DisplayPrices( (ScrapePrices (GetWebpage $url)) )
>
Quote:

> > .\myScript.ps1 "http://books.shop.ebay.com/items/Audiobooks__The-Da-
> > Vinci-Code_W0QQLength87732706ZUnabridged36be1033QQ
> > Condition441b8cfbZBrandNew486d0d47QQ_catrefZ1QQ_dmptZUSQ5fAudiobooksQQ_flnZ1QQ_sacatZ29792QQ_ssovZ1QQ_trksidZp3286Q2ec0Q
> > 2em282"
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Trying to sort then FT an array of HashTables. PowerShell
PowerShell param array PowerShell
Sort Order - Numbers in File and Folder Names Tutorials
IP Addresses sort with Powershell PowerShell
Using a WMI string array in Powershell PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46