How to sort a Multi Dimensional Array in Powershell

梦想的初衷 提交于 2019-12-11 12:37:03

问题


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.

My array looks as below;

$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.


回答1:


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


来源:https://stackoverflow.com/questions/33812045/how-to-sort-a-multi-dimensional-array-in-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!