1

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.

2
  • Have you tried using a custom sort like here? Commented Nov 19, 2015 at 18:51
  • 3
    $Array=$Array|sort {$_[1]} Commented Nov 19, 2015 at 18:57

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

1 Comment

That seems to answer my question , i can assign them back to array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.