2

I have a .txt file something like this:

First, Second, Third, Fourth, -32
First-a, Second-a, Third-a, Fourth-a, 98
First-b, Second-b, Third-b, Fourth-b, 32
First-c, Second-c, Third-c, Fourth-c, 1
First-d, Second-d, Third-d, Fourth-d, -20

I have the following code for sorting:

    $Line = $_.Trim() -Split ','

    New-Object -TypeName PSCustomObject -Property @{
        fst = $Line[0]
        snd = $Line[1]
        thrd = $Line[2]
        frth = $Line[3]
        fifth = [int]$Line[4]
    }

} | Sort-Object -Descending fifth | Write-Host

So this would have to display this:

First-a, Second-a, Third-a, Fourth-a

Since 98 is the highest number

  • As an aside: [`Write-Host` is typically the wrong tool to use](http://www.jsnover.com/blog/2013/12/07/write-host-considered-harmful/), unless the intent is to write _to the display only_, bypassing the success output stream and with it the ability to send output to other commands, capture it in a variable, redirect it to a file. To output a value, use it _by itself_; e.g., `$value` instead of `Write-Host $value` (or use `Write-Output $value`, though that is rarely needed). See also: the bottom section of https://stackoverflow.com/a/50416448/45375 – mklement0 Dec 04 '21 at 19:52
  • @mklement0, I thought that `Write-Host` was somewhat redeemed in 5.0 when it became a wrapper for `Write-Information`. https://stackoverflow.com/a/38593312/447901 – lit Dec 04 '21 at 19:59
  • @lit, `Write-Host` always had its legitimate uses (and since the v5 change you mention you can even capture it, via the information stream), but my comment is meant to highlight the common anti-pattern of using `Write-Host` when the real intent is to output _data_, to the _success stream_. – mklement0 Dec 04 '21 at 20:09
  • @mklement0, I agree that it is generally preferred to keep data in the pipe for better automation/integration capabilities. With no additional information, I did not have the impression that it was a concern of this OP. – lit Dec 04 '21 at 20:16
  • @lit, the reason I thought it was a concern was the `| Write-Host` at the end of the pipeline. Given that `Write-Host` usually produces representations that range from less friendly to downright useless, its use there is pointless. (If the intent is to bypass the success stream for ad-hoc debugging, you'd use `| Out-Host`, which would give you the friendly representations). – mklement0 Dec 04 '21 at 20:28

2 Answers2

3

Since you're only looking for string output, there's no need to construct intermediate [pscustomobject] representations:

Get-Content file.txt | 
  Sort-Object { [int] ($_ -split ', ')[-1] } -Descending |
    Select-Object -First 1 |
      ForEach-Object { $_ -replace ', [^,]+$' }
mklement0
  • 382,024
  • 64
  • 607
  • 775
2

It would seem to be easier to treat this as a CSV file and let the PowerShell CSV functions do more of the work. Field names can be specified on the Import-Csv command.

Import-Csv -Path .\sortcsv.txt -Header @('first','second','third','fourth','fifth') |
    Sort-Object {[int32]$_.fifth} |
    Select-Object -Last 1 |
    ForEach-Object {
        Write-Host "$($_.first), $($_.second), $($_.third), $($_.fourth)"
    }
lit
  • 14,456
  • 10
  • 65
  • 119