如何在 Powershell 中对多维数组进行排序 [英] How to sort a Multi Dimensional Array in Powershell

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

问题描述

我有一个脚本可以定期清理文件夹.每个月都会创建 3-4 个子文件夹;我想要完成的是每月保留一个文件夹,并删除每台服务器上该文件夹上的其余文件夹.我成功地使用了脚本,但遇到了下面的块.

I have a script to cleanup a folder regularly. Every month there are 3-4 sub folders created; what i am trying to accomplish is keep a single folder per month and delete the rest on that folder on every server. I was successfull with the script, but ran into below block.

我的数组如下所示;

$Array = ((Filepath,Timestamp2),(Filepath,Timestamp3),(Filepath,Timestamp1),(Filepath,Timestamp4))

我想做的是,按时间戳对数组中的元素进行排序;怎么做?如果对此有任何其他问题,请告诉我.

What i would like to do is, sort elements in array by timestamp; how to do that? Please let me know if any other questions regarding this.

推荐答案

我的建议是将数组数组转换为自定义对象列表,并按 Timestamp 属性对该列表进行排序:

My recommendation would be to convert the array of arrays into a list of custom objects and sort that list by the Timestamp property:

$array | ForEach-Object {
  New-Object -Type PSCustomObject -Property @{
    Filepath  = $_[0]
    Timestamp = $_[1]
  }
} | Sort-Object Timestamp

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

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