ネストされた返信でコメントを並べ替えるにはどうすればよいですか? 私のデータは次のようになります。Cでのネストされた返信によるコメントの並べ替え#
var comments = new List<Comment>();
comments.Add(AddNewComment(1, null, ""));
comments.Add(AddNewComment(2, null, ""));
comments.Add(AddNewComment(3, null, ""));
comments.Add(AddNewComment(4, 1, ""));
comments.Add(AddNewComment(5, 4, ""));
comments.Add(AddNewComment(6, 1, ""));
comments.Add(AddNewComment(7, null, ""));
comments.Add(AddNewComment(8, 7, ""));
comments.Add(AddNewComment(9, 8, ""));
comments.Add(AddNewComment(10, 9, ""));
comments.Add(AddNewComment(11, 2, ""));
comments.Add(AddNewComment(12, 11, ""));
comments.Add(AddNewComment(13, 1, ""));
comments.Add(AddNewComment(14, 13, ""));
public Comment AddNewComment(int id, int? replyId, string body)
{
return new Comment
{
Id = id,
ReplyId = replyId,
Body = body
};
}
public class Comment
{
public int Id { get; set; }
public int Depth { get; set; }
public string Body { get; set; }
public int? ReplyId { get; set; }
}
は、私のようなものを取得したい:
/*
* 1 =>Depth:0
* -4 =>Depth:1
* --5 =>Depth:2
* -13 =>Depth:1
* --14 =>Depth:2
* -6 =>Depth:1
* 2 =>Depth:0
* -11 =>Depth:1
* --12 =>Depth:2
* 3 =>Depth:0
* 7 =>Depth:0
* -8 =>Depth:1
* --9 =>Depth:2
* ---10 =>Depth:3
* */
私はこれをどのように行うことができますか?
どのように深さを計算しますか?そしてあなたの出力の前には何がありますか? – bc004346