Erlang:用于元组列表的排序或排序函数

3
我有困难对两个相关但是不同的元组列表进行排序。一个列表由表示博客文章的元组列表组成,另一个列表由表示评论帖子的元组列表组成。
问题在于当您想要基于博客 ID 值获得相同的顺序时。博客文章的列表通过日期值排序。因此,您不能仅通过博客 ID 数字排序来排序博客和评论帖子。并且您不能仅通过日期值对评论帖子进行排序,因为博客和相关评论帖子的日期值可能不同。
我不确定如何解决这个问题 - 至少不是以一种优雅的方式。
我应该使用lists:nth,然后获取每个元组列表和位置值吗?然后我会获取博客 ID 的值,然后在评论帖子列表中搜索该 ID。获取该元组列表的值。将该元组列表的值与适当的 nth 位置值关联到一个新列表中。
我应该使用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">>}]]
1个回答

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,

这个函数根据它们相关的博客ID将两个评论 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所能做到的限制吗? - Ben Ahlan
查看 lists:sort/1lists:sort/2 之间的区别。后者接受任意排序函数。 - I GIVE CRAP ANSWERS
但是排序函数本身需要从另一个列表中取出一个元素。但是,似乎只能从同一个列表中取出元素。首先,我在获取每个列表的每个元素的值以进行比较方面遇到了麻烦。 - Ben Ahlan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接