PowerShell 排序数组 [英] PowerShell sort array

查看:83
本文介绍了PowerShell 排序数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数组吐出了这个.

a10
a11
a12
a6
a7
a8
a9

修复它的任何简短/简单的代码:

Any short/simple code to fix it to:

a6
a7
a8
a9
a10
a11
a12

推荐答案

您可以按表达式排序,取第一个字母后的所有内容并将其强制转换为整数:

You can sort by expression, take everything after the first letter and cast it to integer:

$array | sort { [int]$_.substring(1)}

您还可以通过删除任何非数字字符使解决方案更通用:

You can also make the solution more generic by removing any non-digit characters:

$array | sort { [int]($_ -replace '\D')}

这篇关于PowerShell 排序数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆