2010-12-07 262 views
3

我無法排序兩個相關但單獨的元組列表。一個列表由代表博客帖子的元組列表組成。另一個列表由代表評論帖子的元組列表組成。Erlang:排序或排序函數的列表元組列表

問題是,當您想要基於博客ID值的同一訂單。博客帖子的列表通過日期值排序。所以你不能僅僅通過博客ID和在數字上排序博客和評論文章。而你不能僅僅通過日期值排序評論帖子,因爲博客和相關評論帖子的日期值可能不同。

我不知道如何解決這個問題 - 至少不是以優雅的方式。

我應該使用lists:nth並因此得到每個元組列表和位置值?然後我會得到博客ID的價值,然後我會在列表中搜索該ID的評論帖子。獲取該元組列表的值。將新列表中該元組列表的值與適當的第n個位置值相關聯。

我應該使用lists:sort函數嗎?

任何建議與代碼示例非常讚賞。

下面是可以使用的作爲基礎元組列表的兩個示例清單:

[[{<<"blog_id">>,<<"a2">>}, 
    {<<"postDate">>,<<"2010-12-4T6:10:12">>}, 
    {<<"message">>,<<"la di da bo di do">>}], 
[{<<"blog_id">>,<<"b8">>}, 
    {<<"postDate">>,<<"2009-12-3T10:09:33">>}, 
    {<<"message">>,<<"that is cool">>}], 
[{<<"blog_id">>,<<"a9">>}, 
    {<<"postDate">>,<<"2009-12-2T18:12:29">>}, 
    {<<"message">>,<<"i like san francisco">>}]] 


[[{<<"comment_id">>,<<"n6">>}, 
    {<<"related_blog_id">>,<<"b8">>}, 
    {<<"postDate">>,<<"2010-12-5T15:10:12">>}, 
    {<<"message">>,<<"yup really neat">>}], 
[{<<"comment_id">>,<<"y2">>}, 
    {<<"related_blog_id">>,<<"a9">>}, 
    {<<"postDate">>,<<"2009-12-6T10:09:33">>}, 
    {<<"message">>,<<"yes but rent is expensive">>}], 
[{<<"comment_id">>,<<"x4">>}, 
    {<<"related_blog_id">>,<<"a2">>}, 
    {<<"postDate">>,<<"2009-12-5T16:12:29">>}, 
    {<<"message">>,<<"sounds like a hit">>}]] 

和期望的輸出與重排序的第一列表不變和第二列表中的下列:

[[{<<"blog_id">>,<<"a2">>}, 
    {<<"postDate">>,<<"2010-12-4T6:10:12">>}, 
    {<<"message">>,<<"la di da bo di do">>}], 
[{<<"blog_id">>,<<"b8">>}, 
    {<<"postDate">>,<<"2009-12-3T10:09:33">>}, 
    {<<"message">>,<<"that is cool">>}], 
[{<<"blog_id">>,<<"a9">>}, 
    {<<"postDate">>,<<"2009-12-2T18:12:29">>}, 
    {<<"message">>,<<"i like san francisco">>}]] 


[ [{<<"comment_id">>,<<"x4">>}, 
    {<<"related_blog_id">>,<<"a2">>}, 
    {<<"postDate">>,<<"2009-12-5T16:12:29">>}, 
    {<<"message">>,<<"sounds like a hit">>}], 
[{<<"comment_id">>,<<"n6">>}, 
    {<<"related_blog_id">>,<<"b8">>}, 
    {<<"postDate">>,<<"2010-12-5T15:10:12">>}, 
    {<<"message">>,<<"yup really neat">>}], 
[{<<"comment_id">>,<<"y2">>}, 
    {<<"related_blog_id">>,<<"a9">>}, 
    {<<"postDate">>,<<"2009-12-6T10:09:33">>}, 
    {<<"message">>,<<"yes but rent is expensive">>}]] 

回答

3

好吧,新的嘗試:)

我們有:

-module(foo). 
-compile(export_all). 

基本模塊的出口檢驗的東西

blogs() -> 
    [[{<<"blog_id">>,<<"a2">>}, 
     {<<"postDate">>,<<"2010-12-4T6:10:12">>}, 
     {<<"message">>,<<"la di da bo di do">>}], 
    [{<<"blog_id">>,<<"b8">>}, 
     {<<"postDate">>,<<"2009-12-3T10:09:33">>}, 
     {<<"message">>,<<"that is cool">>}], 
    [{<<"blog_id">>,<<"a9">>}, 
     {<<"postDate">>,<<"2009-12-2T18:12:29">>}, 
     {<<"message">>,<<"i like san francisco">>}]]. 

你的博客的定義。

comments() -> 
    [[{<<"comment_id">>,<<"n6">>}, 
     {<<"related_blog_id">>,<<"b8">>}, 
     {<<"postDate">>,<<"2010-12-5T15:10:12">>}, 
     {<<"message">>,<<"yup really neat">>}], 
    [{<<"comment_id">>,<<"y2">>}, 
     {<<"related_blog_id">>,<<"a9">>}, 
     {<<"postDate">>,<<"2009-12-6T10:09:33">>}, 
     {<<"message">>,<<"yes but rent is expensive">>}], 
    [{<<"comment_id">>,<<"x4">>}, 
     {<<"related_blog_id">>,<<"a2">>}, 
     {<<"postDate">>,<<"2009-12-5T16:12:29">>}, 
     {<<"message">>,<<"sounds like a hit">>}]]. 

您對評論的定義。

sorted_comments() -> 
    [[{<<"comment_id">>,<<"x4">>}, 
     {<<"related_blog_id">>,<<"a2">>}, 
     {<<"postDate">>,<<"2009-12-5T16:12:29">>}, 
     {<<"message">>,<<"sounds like a hit">>}], 
     [{<<"comment_id">>,<<"n6">>}, 
     {<<"related_blog_id">>,<<"b8">>}, 
     {<<"postDate">>,<<"2010-12-5T15:10:12">>}, 
     {<<"message">>,<<"yup really neat">>}], 
     [{<<"comment_id">>,<<"y2">>}, 
     {<<"related_blog_id">>,<<"a9">>}, 
     {<<"postDate">>,<<"2009-12-6T10:09:33">>}, 
     {<<"message">>,<<"yes but rent is expensive">>}]]. 

您對排序的定義。

sort(Blogs, Comments) -> 
    %% Create list of blog id's 
    Bs = [proplists:get_value(<<"blog_id">>, B) || B <- Blogs], 

從博客中獲取所有blog_id值。

%% Create the numbering 
    DB = dict:from_list([Item || Item <- lists:zip(Bs, 
          lists:seq(1, length(Bs)))]), 

將博客發生的順序編號。將它們填入字典中以便稍後快速查找。

%% Sorter function: 
    F = fun(I, J) -> 
     II = proplists:get_value(<<"related_blog_id">>, 
        I), 
     JJ = proplists:get_value(<<"related_blog_id">>, 
        J), 
     dict:fetch(II, DB) =< dict:fetch(JJ, DB) 
    end, 

這個功能比較兩個評論,IJ對方根據其相關blog_id。

{Blogs, lists:sort(F, Comments)}. 

返回我們想要返回的內容。

sort_test() -> 
    {blogs(), sorted_comments()} == sort(blogs(), comments()). 

測試儀功能。

2> c(foo). 
{ok,foo} 
3> foo:sort_test(). 
true 
+0

感謝帖子,但實際上通過博客ID排序沒有日期值。基於舊博客文章的評論文章可能比新博客文章中的評論文章更新。並且不能按照我認爲列出的數字排序博客ID:排序僅限於做什麼? – 2010-12-07 19:02:02