2010-12-07 112 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