Not sure if that is a normal data structure for a forum, but it seems super inefficient to have to do a recursive lookup to associate a parent and sub-posts.
$posts = @"
A/A,ID,PID,Updated,Posted,Details
1,8114,8093,N/A,14/02/2020 5:26,2nd Post
2,8107,8093,N/A,14/02/2020 2:03,1st post
3,8203,8197,16/02/2020 17:09,14/02/2020 2:03,1st post edit 3
4,8201,8114,15/02/2020 15:37,14/02/2020 5:26,2nd post edited
5,8197,8108,15/02/2020 13:49,14/02/2020 2:03,1st post edit 2
6,8108,8107,14/02/2020 2:04,14/02/2020 2:03,1st post edit 1
7,8188,8099,N/A,14/02/2020 1:53,3rd post
"@ | ConvertFrom-CSV
$parentPosts = $posts | Where{$posts.ID -notcontains $_.PID}
$results = foreach ( $post in $parentPosts ) {
$subPosts = @()
$lookupId = $post.ID
$i = 1
do {
Write-Host ('Searching a for subposts on {0}' -f $post.ID)
$subPost = $posts | Where{$_.PID -eq $lookupId}
if ($subPost) {
Write-Host ('Found sub post {0}' -f $subPost.ID)
$subPosts += $subPost | Select *, @{Name='SubPostSequence';Expression={$i}}
$lookupId = $subPost.Id
$i++
}
}
until (!$subPost)
$post | Select *,
@{Name='SubPosts';Expression={$subPosts}}
}
Output:
Searching a for subposts on 8114
Found sub post 8201
Searching a for subposts on 8201
Searching a for subposts on 8107
Found sub post 8108
Searching a for subposts on 8108
Found sub post 8197
Searching a for subposts on 8197
Found sub post 8203
Searching a for subposts on 8203
Searching a for subposts on 8188
PS C:\Users\rasim> $results
A/A : 1
ID : 8114
PID : 8093
Updated : N/A
Posted : 14/02/2020 5:26
Details : 2nd Post
SubPosts : @{A/A=4; ID=8201; PID=8114; Updated=15/02/2020 15:37; Posted=14/02/2020 5:26; Details=2nd post edited; SubPostSequence=1}
A/A : 2
ID : 8107
PID : 8093
Updated : N/A
Posted : 14/02/2020 2:03
Details : 1st post
SubPosts : {@{A/A=6; ID=8108; PID=8107; Updated=14/02/2020 2:04; Posted=14/02/2020 2:03; Details=1st post edit 1; SubPostSequence=1}, @{A/A=5; ID=8197; PID=8108; Updated=15/02/2020 13:49; Posted=14/02/2020 2:03; Details=1st post edit 2;
SubPostSequence=2}, @{A/A=3; ID=8203; PID=8197; Updated=16/02/2020 17:09; Posted=14/02/2020 2:03; Details=1st post edit 3; SubPostSequence=3}}
A/A : 7
ID : 8188
PID : 8099
Updated : N/A
Posted : 14/02/2020 1:53
Details : 3rd post
SubPosts :
PS C:\Users\rasim> $results[1].SubPosts
A/A : 6
ID : 8108
PID : 8107
Updated : 14/02/2020 2:04
Posted : 14/02/2020 2:03
Details : 1st post edit 1
SubPostSequence : 1
A/A : 5
ID : 8197
PID : 8108
Updated : 15/02/2020 13:49
Posted : 14/02/2020 2:03
Details : 1st post edit 2
SubPostSequence : 2
A/A : 3
ID : 8203
PID : 8197
Updated : 16/02/2020 17:09
Posted : 14/02/2020 2:03
Details : 1st post edit 3
SubPostSequence : 3
PS C:\Users\rasim> $results[1].SubPosts | Sort SubPostSequence -Descending
A/A : 3
ID : 8203
PID : 8197
Updated : 16/02/2020 17:09
Posted : 14/02/2020 2:03
Details : 1st post edit 3
SubPostSequence : 3
A/A : 5
ID : 8197
PID : 8108
Updated : 15/02/2020 13:49
Posted : 14/02/2020 2:03
Details : 1st post edit 2
SubPostSequence : 2
A/A : 6
ID : 8108
PID : 8107
Updated : 14/02/2020 2:04
Posted : 14/02/2020 2:03
Details : 1st post edit 1
SubPostSequence : 1
If you convert the Updated\Posted date strings into [datetime], you could do some date sorts (last post\last updated post), but I’d rather just have a simple numeric sequence for sorting. The -Descending just sorts based on the latest to oldest. The data to me is very odd, be curious to see what the actual query is in the data layer to return a forum thread.
@Amala12: Please start your own thread to get your question answered