Java 根据数组列表中的父值对注释和回复进行排序(嵌套注释)

Java 根据数组列表中的父值对注释和回复进行排序(嵌套注释),java,android,kotlin,Java,Android,Kotlin,我有一个评论和回复列表。如果注释是回复,则它是具有父值的标识。我希望注释位于回复的顶部。(我希望嵌套注释) 这是我的清单: sortedComment.add(CommentTest(id=1, parent=0)) sortedComment.add(CommentTest(id=2, parent=0)) sortedComment.add(CommentTest(id=3, parent=0)) sortedComment.add(CommentTest(i

我有一个评论和回复列表。如果注释是回复,则它是具有父值的标识。我希望注释位于回复的顶部。(我希望嵌套注释) 这是我的清单:

    sortedComment.add(CommentTest(id=1, parent=0))
    sortedComment.add(CommentTest(id=2, parent=0))
    sortedComment.add(CommentTest(id=3, parent=0))
    sortedComment.add(CommentTest(id=4, parent=1))
    sortedComment.add(CommentTest(id=5, parent=2))
    sortedComment.add(CommentTest(id=6, parent=3))
    sortedComment.add(CommentTest(id=7, parent=6))
    sortedComment.add(CommentTest(id=8, parent=6))
    sortedComment.add(CommentTest(id=9, parent=2))
    sortedComment.add(CommentTest(id=10, parent=5))
    sortedComment.add(CommentTest(id=11, parent=5))
    sortedComment.add(CommentTest(id=12, parent=13))
    sortedComment.add(CommentTest(id=13, parent=4))
我想把它们分类如下:

    1 -> comment
    4 --> replay for 1
    13 --> replay for 4
    12 --> replay for 13
    2 -> comment
    9 --> replay for 2
    5 --> replay for 2
    10 -->replay for 5
    11 --> replay for 5
    3 -> comment
    6 --> replay for 3
    7 --> replay for 6
    8 --> replay for 6
我已经有了一些解决方案,但它不起作用。以下是我的解决方案:

解决方案1:

    var index = -1
    var replyIndex: Int
    sortedComment.forEach { comment ->
        replyIndex = -1
        index++
        if (comment.parent != 0) {
            sortedComment.forEach { reply ->
                replyIndex++
                if (comment.parent == reply.id) {
                        Collections.swap(sortedComment, index, replyIndex+1 )
                }
            }
        }
    }
解决方案2:

        for (comment in sortedComment){
        if (sortedComment2.size==sortedComment.size) break
        if (comment.parent==0)
            sortedComment2.add(comment)
            sortedComment.forEach { reply->
            if (comment.id==reply.parent){
                sortedComment2.add(reply)
                sortedComment.forEach {cg->
                    if (reply.id==cg.parent)
                        sortedComment2.add(cg)
                }
            }
        }
    }

如果有人能帮上忙,我将不胜感激。

您需要的东西如下:

ArrayList <CommentTest> sortedList = new ArrayList<>();
for(int i = 0; i < sortedComment.size(); i++) {
    CommentTest comment = sortedComment.get(i);
    if(comment.getParent() == 0) {
      sortedList.add(comment);
      LinkedList<Integer> to_search = new LinkedList<Integer>();
      to_search.addFirst(comment.getId());
      while(!to_search.isEmpty()){
          for(int j = i + 1; j < sortedComment.size(); j++) {
              CommentTest c = sortedComment.get(j);
              if(c.getParent() == to_search.getFirst()) {
                 sortedList.add(c);
                 to_search.addLast(c.getId());
               }
          }
          to_search.removeFirst();
      }
    }
}
而不是重赛拳头的重赛。但是,如果这不是您正在使用的排序标准的硬约束,并且像这样的排序:
1 4 13 12 2 5 10 11 9 3 6 7 8
是有效的。然后我们可以将代码简化为更简单的内容,例如:

void sort_comments(List <CommentTest> unsorted, List <CommentTest> sorted, int parentID){
    for (CommentTest c :  unsorted){
         if (c.getParent() == parentID){
             sorted.add(c);
             sort_comments(unsorted, sorted, c.getId());
         }
    }
}
void sort_注释(列表未排序、列表排序、int parentID){
用于(测试c:未排序){
if(c.getParent()==parentID){
添加(c);
sort_注释(未排序、已排序、c.getId());
}
}
}
或者更优雅地使用流:

void sort_comments(List <CommentTest> unsorted, List<CommentTest> sorted, int parentID){
  unsorted.stream().filter(c -> c.getParent() == parentID).forEach(c -> { sorted.add(c); 
        sort_comments(unsorted, sorted, c.getId()); });
}
void sort_注释(列表未排序、列表排序、int parentID){
unsorted.stream().filter(c->c.getParent()==parentID).forEach(c->{sorted.add(c);
排序_注释(未排序,已排序,c.getId();});
}
//创建映射{parent->list of children}
// {0=[1, 2, 3], 1=[4], 2=[5, 9], 3=[6], 6=[7, 8], 5=[10, 11], 13=[12], 4=[13]}
val map=sortedComment.groupBy({it.parent},{it.id})
//递归地添加子对象的ID
//例如,如果key==2,它将从[5,9]中添加5,然后从[10,11]中添加10,
//然后是11,然后返回到[5,9],取9(结果[5,10,11,9])
趣味助手(key:Int):List=map[key]?.flatMap{listOf(it)+helper(it)}?:listOf()
val result=helper(0)/[1,4,13,12,2,5,10,11,9,3,6,7,8]

谢谢大家。我想我有一个性能更好的面糊解决方案:

private fun sortComment(parentId: String,sortedList:ArrayList<Item>){
    for (comment in unsortedList){
        if (sortedList.size==unsortedList.size) break
        if (comment.parent!=parentId)
            continue
        sortedList.add(comment)
        sortComment(comment.id!!,sortedList)
    }
}
private-fun-sortComment(parentId:String,sortedList:ArrayList){
for(未排序列表中的注释){
如果(sortedList.size==unsortedList.size)中断
if(comment.parent!=parentId)
持续
sortedList.add(注释)
sortComment(comment.id!!,sortedList)
}
}
对于任何对嵌套注释有相同问题的人来说,这是一个很好的排序方法

// creates map {parent -> list of children}
// {0=[1, 2, 3], 1=[4], 2=[5, 9], 3=[6], 6=[7, 8], 5=[10, 11], 13=[12], 4=[13]}
val map = sortedComment.groupBy({ it.parent }, { it.id })

// recursively adds the ids of children
// for example if key == 2 it will add 5 from [5, 9] then 10 from [10, 11], 
// then 11, then it will go back to [5, 9] and will take 9 (result [5, 10, 11, 9])
fun helper(key: Int): List<Int> = map[key]?.flatMap { listOf(it) + helper(it) } ?: listOf()

val result = helper(0) // [1, 4, 13, 12, 2, 5, 10, 11, 9, 3, 6, 7, 8]
private fun sortComment(parentId: String,sortedList:ArrayList<Item>){
    for (comment in unsortedList){
        if (sortedList.size==unsortedList.size) break
        if (comment.parent!=parentId)
            continue
        sortedList.add(comment)
        sortComment(comment.id!!,sortedList)
    }
}