Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# PowerShell排序顺序不正确_C#_Powershell_Sorting - Fatal编程技术网

C# PowerShell排序顺序不正确

C# PowerShell排序顺序不正确,c#,powershell,sorting,C#,Powershell,Sorting,因此,我尝试使用PowerShell对文件顶部的C#“using”语句进行排序。对于给定的输入文件file.cs,using语句如下所示: using System.Reflection; using System.Configuration; using System.Runtime.Caching; using System.Linq; using System; 我希望输出将“usingsystem”作为第一个“using”,但实际上Sort对象将其排序到底部。如何将其更改为排序到列表的

因此,我尝试使用PowerShell对文件顶部的C#“using”语句进行排序。对于给定的输入文件file.cs,using语句如下所示:

using System.Reflection;
using System.Configuration;
using System.Runtime.Caching;
using System.Linq;
using System;
我希望输出将“usingsystem”作为第一个“using”,但实际上Sort对象将其排序到底部。如何将其更改为排序到列表的顶部

function Update-UsingStatements
{
  param (
    [Parameter(Mandatory=$true)][string]$FilePath
  )

  $fileLines = Get-Content $FilePath
  $contents = $fileLines | Out-String

  $list = New-Object 'System.Collections.Generic.List[string]'
  $contents | Select-String -pattern 'using\s[\w\.]+;' -AllMatches | ForEach-Object {$_.Matches} | ForEach-Object { $list.Add($_.Value) }
  $list = $list | Sort-Object

  for ($i = 0; $i -lt $list.Count; $i++)
  {
    $fileLines[$i] = $list[$i]
  }

  $fileLines | Out-File $FilePath -Encoding utf8
}

您得到的排序顺序是因为字符串包含尾随的
,并在字符表中
(ASCII字符59)位于
(ASCII字符46)之后。因此,排序顺序是绝对正确的,即使它不是您所期望的

从用于排序的属性中删除尾随分号,以解决以下问题:

$list = $list | Sort-Object { $_ -replace ';' }

使用Ansgar Wiechers的排序调整作为主要修复方法,您可以在代码的其余部分中消除许多旧世界的计数扭曲:

function Update-UsingStatements
{
  param (
    [Parameter(Mandatory=$true)][string]$FilePath
  )

  # Separate the 'using' statements from everything else
  $using, $rest = (Get-Content -Path $FilePath).where({$_ -match '^using '}, 'split')

  # sort the 'using' statements, with a tweak to bring 'using system;' to the top
  $using = $using | Sort-Object -Property { $_ -replace ';' }

  # output sorted 'using' statements and rest of file, over the original file
  $using, $rest | Set-Content -Path $FilePath -Encoding UTF8

}

我已经很晚了,但为你的10万销售代表成绩而庆祝。请接受谦卑的我的尊重。我也有同样的想法,但我打算用
$\u.TrimEnd(“;”)
而不是
-replace
。作为一个单独的答案发表似乎是一种浪费,所以我只是在你的评论中添加了一些选项。回答安斯加,干得好@sodawillow谢谢你。我是一名C#程序员,正在努力拼凑一个PowerShell脚本——我感谢你的改进。更加优雅+1为“你的旧世界计算扭曲”