title: Sorting Listview control columns with integer values in Powershell forms
body: <p>I am trying to sort the column &quot;Size (kb)&quot; in the following Listview. The &quot;Size (kb)&quot; column is getting sorted on basis of the text value which is not the intended result. I am getting correct results for all other columns</p>
  <pre><code># Defining the listbox fo displaying the search results
  $lvw_resultsListView = New-Object System.Windows.Forms.ListView -Property @{
      View        = [System.Windows.Forms.View]::Details
      Location    = New-Object System.Drawing.Size(10, 40)  
      Width       = 700
      Height      = 350
      Scrollable  = $true
      MultiSelect = $true
      Font        = 'Arial,10'
      AutoSize    = $true
      Sorting     = &quot;Ascending&quot;
      FullRowSelect = $true
      GridLines = $True
  }
  </code></pre>
  <p>Here is the code that adds columns to the Listview</p>
  <pre><code># Adding Columns to List View
  $lvw_resultsListView.columns.Add(&quot;Filename&quot;, 150) | Out-Null
  $lvw_resultsListView.columns.Add(&quot;Path&quot;, 340) | Out-Null
  $lvw_resultsListView.columns.Add(&quot;Size (kb)&quot;, 90) | Out-Null
  $lvw_resultsListView.columns.Add(&quot;Created on&quot;, 70) | Out-Null
  $lvw_resultsListView.columns.Add(&quot;Ext&quot;, 60) | Out-Null
  </code></pre>
  <p>Here is the code that adds values from  a file object</p>
  <pre><code># Clearing list view items
  $lvw_resultsListView.Items.Clear()
  # Adding file information to the list bx by cycling through each file
  foreach ($file in $global:files) {
      $fileItem = New-Object System.Windows.Forms.ListViewItem( $file.BaseName)
      $fileItem.Subitems.Add($file.FullName) | Out-Null
      $fileItem.Subitems.Add($file.Length) | Out-Null
      $tempCreationDate = $file.CreationTime | Get-Date -f &quot;yyyy-MM-dd&quot;
      $fileItem.Subitems.Add($tempCreationDate) | Out-Null
      $fileItem.Subitems.Add($file.Extension) | Out-Null
      $lvw_resultsListView.Items.Add($fileItem) | Out-Null
  }
  </code></pre>
  <p>And here is the sorting function that is called on column click</p>
  <pre><code>function SortListView {
      Param(
          [System.Windows.Forms.ListView] $senderPsItem,
          $column
      )
      $temp = $senderPsItem.Items | Foreach-Object { $_ }
      $Script:SortingDescending = !$Script:SortingDescending
      $senderPsItem.Items.Clear()
      $senderPsItem.ShowGroups = $false
      $senderPsItem.Sorting = 'none'
      
      if($column.Text -eq &quot;Size (kb)&quot;) {
          
          $senderPsItem.Items.AddRange(($temp | Sort-Object -Descending:$script:SortingDescending -Property @{ Expression={ [int]$_.SubItems[$column].Text } }))
          
      } else {
          $senderPsItem.Items.AddRange(($temp | Sort-Object -Descending:$script:SortingDescending -Property @{ Expression={ $_.SubItems[$column].Text } }))
  
      }
  }
  </code></pre>
  <p>Here(snippet from the results form) is a sample result I am getting for the &quot;Size (kb)&quot; column</p>
  <p><a href="https://i.stack.imgur.com/kh7JI.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/kh7JI.png" alt="enter image description here" /></a></p>
  <p>Here is the code calling sorting function</p>
  <pre><code>$lvw_resultsListView.Add_ColumnClick({SortListView $this $_.Column})
  </code></pre>
  <p>Could someone suggest a way to improve my sort function to properly sort the integer column. I tried converting the test to integer by using [int] method as may be seen from the sorting function?
  Thank you in advance!</p>