Last active
August 29, 2015 13:57
-
-
Save denizozger/9926025 to your computer and use it in GitHub Desktop.
Sort a list of comments in date and reply order
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Given a list of random comments, having dates and parentIds, this code | |
* sorts the list following parent-child relationships and dates. | |
* | |
* Notation: | |
* C1R2 stands for 'Second reply to first comment' | |
* C2R2R1R1 stands for '1st Reply to the 1st Reply to the | |
* 2nd Reply of the 2nd Comment' | |
* | |
* Input: | |
* | |
* C1 | |
* C2 | |
* C2R1 | |
* C3 | |
* C2R1R1 | |
* C2R2 | |
* C4 | |
* C2R2R1 | |
* C2R2R2 | |
* C3R1 | |
* C2R2R1R1 | |
* C2R2R3 | |
* C4R1 | |
* C4R2 | |
* | |
* Output: | |
* | |
* C4 | |
* C4R1 | |
* C4R2 | |
* C3 | |
* C3R1 | |
* C2 | |
* C2R1 | |
* C2R1R1 | |
* C2R2 | |
* C2R2R1 | |
* C2R2R1R1 | |
* C2R2R2 | |
* C2R2R3 | |
* C1 | |
* | |
*/ | |
var comment = function(id, date, parentId, text) { | |
this.id = id; | |
this.date = date; | |
this.parentId = parentId; | |
this.text = text; | |
} | |
var comments = []; | |
/** | |
* Users enter comments. | |
* C1R2 stands for 'Second reply to first comment' | |
*/ | |
comments.push(new comment(1, 2001, null, 'C1')); | |
comments.push(new comment(2, 2002, null, 'C2')); | |
comments.push(new comment(3, 2003, 2, 'C2R1')); | |
comments.push(new comment(4, 2004, null, 'C3')); | |
comments.push(new comment(5, 2005, 3, 'C2R1R1')); | |
comments.push(new comment(6, 2006, 2, 'C2R2')); | |
comments.push(new comment(7, 2007, null, 'C4')); | |
comments.push(new comment(8, 2008, 6, 'C2R2R1')); | |
comments.push(new comment(9, 2009, 6, 'C2R2R2')); | |
comments.push(new comment(10, 2010, 4, 'C3R1')); | |
comments.push(new comment(11, 2011, 8, 'C2R2R1R1')); | |
comments.push(new comment(12, 2012, 6, 'C2R2R3')); | |
comments.push(new comment(13, 2013, 7, 'C4R1')); | |
comments.push(new comment(14, 2014, 7, 'C4R2')); | |
// Sort the comments in descending date order | |
sortCommentsByDate(comments, true); | |
// Generate a tree structure | |
var commentsInTreeForm = buildTree(comments, null); | |
// Remove replies from first level of comments | |
commentsInTreeForm = removeRepliesFromFirstLevel(commentsInTreeForm); | |
// Transform the tree structure to a list | |
var commentArray = getArrayFromCommentTree(commentsInTreeForm); | |
// Output the result | |
console.log(commentArray); | |
/** | |
* Implementation | |
*/ | |
function buildTree(comments, parentId) { | |
var output = []; | |
for (var i = 0; i < comments.length; i++) { | |
var comment = comments[i]; | |
if (comment.parentId === parentId) { | |
comment.children = buildTree(comments, comment.id); | |
output.push(comment); | |
} | |
} | |
if (parentId) { | |
// First level of comments are in desc date order, replies in asc date | |
sortCommentsByDate(output, false); | |
} | |
return output; | |
} | |
function sortCommentsByDate(comments, desc) { | |
comments.sort(function (first, second) { | |
if (desc) { | |
return second.date - first.date; | |
} else { | |
return first.date - second.date; | |
} | |
}); | |
} | |
function removeRepliesFromFirstLevel(comments) { | |
var newComments = []; | |
for (var i = 0; i < comments.length; i++) { | |
var comment = comments[i]; | |
if (!comment.parentId) { | |
newComments.push(comment); | |
} | |
} | |
return newComments; | |
} | |
function getArrayFromCommentTree(comments, commentArray) { | |
if (!commentArray) { | |
commentArray = []; | |
} | |
for (var i = 0; i < comments.length; i++) { | |
var comment = comments[i]; | |
commentArray.push(comment); | |
if (comment.children.length !== 0) { | |
getArrayFromCommentTree(comment.children, commentArray); | |
} | |
} | |
return commentArray; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment