[英]Sorting comments with nested replies in 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
* */
我怎樣才能做到這一點?
為此,您將需要創建一個分層的排序算法。 現在,真正的問題是您是否要打印它或將其包含在以這種方式排序的另一個集合中。
首先,我修改了注釋集合以在Children
屬性中包含子Comment
項目。
public class Comment
{
/// <summary>
/// gets the child comments
/// </summary>
public IList<Comment> Children { get; } = new List<Comment>();
public int Id { get; set; }
public int Depth { get; set; }
public string Body { get; set; }
public int? ReplyId { get; set; }
}
現在,使用與您相同的代碼,我創建了一個簡單的枚舉器系統,該系統可以為當前注釋創建子級。 這是基於ReplyId
的值和父代的ReplyId == Id
的位置。 即。 Id:4 Maps as a child to Id:1
public static void EnumerateTree(Comment comment, int depth, IEnumerable<Comment> collection)
{
comment.Depth = depth;
foreach(var child in collection.Where(c => c.ReplyId.HasValue && c.ReplyId == comment.Id))
{
comment.Children.Add(child);
EnumerateTree(child, depth + 1, collection);
}
}
所以這是非常基本的,需要一個comment
,即父注釋。 深度是當前深度的從0開始的索引。 最后收集評論。 這是通過首先設置評論的深度來實現的。 然后,它將找到映射到該comment
所有子項(父項)。 接下來,迭代所有這些注釋,將其添加到父級Children
屬性中,然后為該子級調用EnumerateTree
方法。
最后,我們將其放置在您的主類中(在所有添加注釋的內容下)。
var sorted = new List<Comment>();
foreach(var comment in comments.Where(x => x.ReplyId == null)) //root comments do not have a reply id
{
sorted.Add(comment);
EnumerateTree(comment, 0, comments);
}
最后,您將獲得一個基於層次結構的數據視圖。
您需要一個遞歸算法。 請嘗試以下代碼:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Comment comment = new Comment();
comment.Initialize();
comment.SortComments();
comment.PrintSortedComments();
}
}
public class Comment
{
public static List<Comment> comments = new List<Comment>();
public List<Comment> sortedComments = null;
public int Id { get; set; }
public int Depth { get; set; }
public string Body { get; set; }
public int? ReplyId { get; set; }
public void Initialize()
{
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 void SortComments()
{
sortedComments = new List<Comment>();
List<Comment> levelZeroComments = comments.Where(x => x.ReplyId == null).OrderBy(x => x.Id).ToList();
foreach (Comment comment in levelZeroComments)
{
sortedComments.Add(comment);
comment.Depth = 0;
RecusiveSort(comment.Id, 1);
}
}
public void RecusiveSort(int id, int depth)
{
List<Comment> childComments = comments.Where(x => x.ReplyId == id).OrderBy(x => x.Id).ToList();
foreach (Comment comment in childComments)
{
sortedComments.Add(comment);
comment.Depth = depth;
RecusiveSort(comment.Id, depth + 1);
}
}
public void PrintSortedComments()
{
Console.WriteLine("/*");
foreach (Comment sortComment in sortedComments)
{
Console.WriteLine(" * {0}{1}", new string('-', sortComment.Depth), sortComment.Id);
}
Console.WriteLine("* */");
Console.ReadLine();
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.