I have a list with comments and replies. if a comment is a reply it is identity with parent value. I want comments to be on top of replies.(I want nested comments)
This is my list:

```
    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))

```

I want to sort them like this:

```
    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

```

I already have some solution but it does not work. These are my solutions:

solution 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 )
                }
            }
        }
    }

```

solution 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)
                }
            }
        }
    }

```

I would be really grateful if anyone could help.