ios - Sort NSMutableArray as threaded comments -


i'm developing ios app website, uses disqus comment system. i'm trying add feature adding comments application.

my problem don't know how keep threaded structure, example:

  1. comment 1
    • child 1.1
      • child 1.1.1
      • child 1.1.2
    • child 1.2
      • child 1.2.1
  2. comment 2
    • child 2.1

i found solution in java here java solution

it's works, there big problem. makes structure this:

  1. comment 1
    • child 1.1
    • child 1.2
      • child 1.2.1
      • child 1.1.1
      • child 1.1.2
  2. comment 2
    • child 2.1

it ruins relations between parents , children.
when add new child, knows how many children parent has,in other words how many brothers child has. there no information brother of child has children, in other words when add new child doesn't know has nephews.

[threaded insertobject:child atindex:i+[parent childcount]]; 

please me this, i'm stuck.

here link, test project link

and here code of sorting array in objective-c

+ (nsmutablearray *)makethreadedcommentsoutof:(nsmutablearray *)comments{      nsmutablearray *threaded = [[nsmutablearray alloc] initwithcapacity:0];      //an array used hold processed comments should removed @ end of cycle     nsmutablearray *removecomments = [[nsmutablearray alloc] initwithcapacity:0];      //get root comments first (comments no parent)     for(int = 0; < [comments count]; i++){         comment *comment = [comments objectatindex:i];         if([[comment parentid] isequal:[nsnull null]]){             comment.commentdepth = 0;//a property of comment hold depth             comment.childcount = 0;             [threaded addobject:comment];             [removecomments addobject:comment];         }     }      if([removecomments count] > 0){         //clear processed comments         [comments removeobjectsinarray:removecomments];         [removecomments removeallobjects];     }      int depth = 0;     //get child comments max depth of 10     while([comments count] > 0 && depth <= 10){         depth++;         for(int j = 0; j< [comments count]; j++){             comment *child = [comments objectatindex:j];             //check root comments match             for(int = 0; < [threaded count]; i++){                 comment *parent = [threaded objectatindex:i];                 if([[parent commentid] isequaltostring:[child parentid]]){                     [parent setchildcount:[parent childcount]+1 ];                     [child setcommentdepth:depth+[parent commentdepth]];                     [threaded insertobject:child atindex:i+[parent childcount]];                     [removecomments addobject:child];                     continue;                     //break;                 }             }         }         if([removecomments count] > 0){             //clear processed comments             [comments removeobjectsinarray:removecomments];             [removecomments removeallobjects];         }     }         return threaded; } 

though not appears comments in array in temporal order; children occur after parents , siblings come before them.

your issue comment not come after previous sibling , children, or after parent if first child.

you not if comments know commentid of children, know commentid of parent.

if of above wrong rest of answer won't you. assuming correct consider following rough algorithm:

  1. create empty array hold root comments, without parents.
  2. create empty dictionary map parent commentid's arrays of child indexes in array of comments.
  3. make 1 pass on array of comments examining each comment in turn. if comment has no parent add array of root comments. if has parent add comment's index dictionary maps parent commentid's arrays of child indexes.

with these 2 data structures can traverse comments in order wish. step through array of root comments, , every comment step through children stepping through array of child indexes found in dictionary. , on, recursively.

if want "sorted" array can create using above recursive algorithm, no need try calculate insert comment within existing mutable array - challenging task found out - comments added in right order.

hth

addendum: response comment

recursion fundamental concept in programming. in pseudo-code outline algorithm described above is:

visitalldescendants(comment - parent comment                     allcomments - indexed collection of comments                     parenttochildren - map parentid child comment indexes                    ) {    comment - whatever want parent    every childindex in order parenttochildren[comment]       visitalldescendants(allcomments[childindex], allcomments, parenttochildren) - recursively process children } 

you call each of roots.

if "do comment" add ordered collection algorithm produce collection of comments in order want.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -