亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

搜索
ios數(shù)組對(duì)象排序 就是類似QQ評(píng)論和回復(fù) 服務(wù)器是按照時(shí)間排的 我現(xiàn)在要排序回復(fù)插到回復(fù)的那條評(píng)論下面
迷茫
迷茫 2017-04-17 16:54:22
[iOS討論組]
迷茫
迷茫

業(yè)精于勤,荒于嬉;行成于思,毀于隨。

全部回復(fù)(1)
天蓬老師

代碼用C#容易寫(xiě),你需要的時(shí)候自己轉(zhuǎn)成oc或swift

你要兩個(gè)數(shù)據(jù)結(jié)構(gòu):

class Comment
{
    public string id;
    public string message;
    puglic Comment parent;
    puglic List<Comment> Children;
}

Dictionary<string, Comment> comments = {};

假設(shè)說(shuō)你拿到的已經(jīng)是按照時(shí)間排序好的了,那首先你就可以寫(xiě)一個(gè)函數(shù)來(lái)生成他們的父子結(jié)構(gòu)。這里的ServerComment指的是你從服務(wù)器拿到的數(shù)據(jù),顯然這不可能是一棵樹(shù),所以你要聲明上面的Comment類,注意區(qū)別這兩個(gè)類型。

struct ServerComment
{
    string id;
    string parentId; // nullable
    string message;
}
void Resolve(ServerComment[] commentsByTime)
{
    foreach(var serverComment in commentsByTime)
    {
        var comment = new Comment()
        {
            id = serverComment.id,
            message = serverComment.message
        };
        comments.Add(comment.id, comment);
    }
    foreach(var serverComment in commentsByTime)
    {
        if(serverComment.parentId != null)
        {
            var comment = comments[serverComment.id];
            var parent = comments[serverComment.parentId];
            if (parent.Children == null)
            {
                parent.Children = new List<Comment>();
            }
            comment.parent = parent;
            parent.Children.Add(comment);
        }
    }
}

然后你就得到了他們的父子關(guān)系了。后面排序就簡(jiǎn)單了,只需要按照時(shí)間順序找到所有parent==null的記錄,然后立刻把他的Children枚舉出來(lái)就可以了??梢允褂眠f歸來(lái)做:

void PrintTree(Comment comment, List<Comment> sortedComments)
{
    sortedComments.Add(comment);
    if(comment.Children != null)
    {
        foreach(var child in comment.Children)
        {
            Sort(child.id, sortedComments)
        }
    }
}

List<Comment> Sort(ServerComment[] commentsByTime)
{
    List<Comment> sortedComments = new List<Comment>();
    foreach(var serverComment in commentsByTime)
    {
        var comment=comments[serverComment.Id];
        if (comment.parent == null)
        {
            PrintTree(comment, sortedComments);
        }
    }
    return sortedComments;
}

就大功告成了。如果你需要打印出真的樹(shù)形的表格,那只要改改PrintTree就行了。

==============================================================

如果題主看不明白的話,下面就是代碼的主要內(nèi)容。

假設(shè)你拿到的數(shù)據(jù)有

(a, null, x) (b, null, y) (c, a, z) (d, a, u)

他們已經(jīng)排序好了
那么最終comments這個(gè)map的內(nèi)容就是

a => (a, null, x, children = [(c, a, z), (d, a, u)])
b => (b, null, y)
c => (c, a, z)
d => (d, a, u)

注意c跟d是class,所以他們跟a的children里面是同一個(gè)對(duì)象。

不過(guò)這個(gè)map是按照id排序的,不是按照時(shí)間排序的,所以才會(huì)有后面那個(gè)函數(shù),重新讀一下服務(wù)器給你的comment,去找那些parent是null的(在這里就是a和b),最后打印出來(lái):

(a, x)
    (c, z)
    (d, u)
(b, y) 
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板
關(guān)于我們 免責(zé)申明 意見(jiàn)反饋 講師合作 廣告合作 最新更新
php中文網(wǎng):公益在線php培訓(xùn),幫助PHP學(xué)習(xí)者快速成長(zhǎng)!
關(guān)注服務(wù)號(hào) 技術(shù)交流群
PHP中文網(wǎng)訂閱號(hào)
每天精選資源文章推送
PHP中文網(wǎng)APP
隨時(shí)隨地碎片化學(xué)習(xí)
PHP中文網(wǎng)抖音號(hào)
發(fā)現(xiàn)有趣的

Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)