【问题标题】:Erlang: Sorting or Ordering Function for List of Tuple ListsErlang:元组列表列表的排序或排序函数
【发布时间】:2011-05-21 18:30:53
【问题描述】:

我无法对两个相关但独立的元组列表进行排序。一个列表由表示博客文章的元组列表组成。另一个列表由表示评论帖子的元组列表组成。

问题是当您想要基于博客 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">>}]]

【问题讨论】:

    标签: list sorting erlang tuples


    【解决方案1】:

    好的,那么新的尝试:)

    我们有:

    -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">>}]].
    

    你对 cme​​ts 的定义。

    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,
    

    此函数根据相关的 blog_id 比较两个 Comments,IJ

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

    返回我们想要返回的内容。

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

    测试功能。

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

    【讨论】:

    • 感谢您的帖子,但实际上是通过博客 ID 而非日期值排序的。基于旧博客帖子的评论帖子可能比新博客帖子的评论帖子更新。并且不能以数字方式对博客 ID 进行排序,这就是我认为列表:排序仅限于做的事情?
    • 查看lists:sort/1lists:sort/2 之间的区别。后者采用任意排序函数。
    • 但是排序函数本身需要从另一个列表中获取一个元素。但是,您似乎只能从同一个列表中获取元素。获取每个列表的每个元素的值以进行比较是我首先遇到的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-10-14
    • 2017-02-04
    • 2011-07-25
    • 2014-05-11
    • 2021-09-11
    • 1970-01-01
    • 2011-07-16
    相关资源
    最近更新 更多