PoshCode Logo PowerShell Code Repository

Sort-ISE (modification of post by view diff)
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/792"></script>download | new post

This function sorts the open editor pane starting at a specific column using the next length characters. It would be nice to wrap it with a small function showing a dialog to querry this values and to put this into the custom menue.

  1. function Sort-ISE ()
  2. {
  3.  <#
  4. .SYNOPSIS
  5.     ISE Extension sort text starting at column $start comparing the next $length characters    
  6. .DESCRIPTION
  7.     ISE Extension sort text starting at column $start comparing the next $length characters
  8.     Leftmost column is column 1    
  9. .NOTES
  10.     File Name  : Sort_ISE.ps1
  11.     Author     : Bernd Kriszio - http://pauerschell.blogspot.com/
  12.     Requires   : PowerShell V2 CTP3
  13. .LINK
  14.     Script posted to:
  15. .EXAMPLE
  16.     Sort-ISE  1 10
  17. #>
  18. #requires -version 2.0
  19.     param (    
  20.         [Parameter(Position = 0, Mandatory=$false)]
  21.         [int]$start = 1,    
  22.         [Parameter(Position = 1, Mandatory=$false)]
  23.         [int]$length = -1    
  24.     )    
  25.     $newlines = @{}
  26.  
  27.     $editor = $psISE.CurrentOpenedFile.Editor
  28.     $caretLine = $editor.CaretLine
  29.     $caretColumn = $editor.CaretColumn
  30.     $text = $editor.Text.Split("`n")
  31.     foreach ($line in $text){
  32.         $key = [string]::join("", $line[($start - 1)..($start - 1 + $length)])
  33.         if ( $newlines[$key]  -ne $null) {
  34.             $newlines[$key] =  $newlines[$key] + $line  
  35.         }
  36.         else {
  37.             $newlines[$key] =  $line
  38.         }
  39.     }
  40.     $Sortedkeys = $newlines.keys | sort
  41.     $newtext = ''
  42.     foreach ($key in $Sortedkeys){
  43.         $newtext += $newlines[$key]
  44.     }
  45.     $editor.Text = $newtext
  46. }

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me