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

发布于 2024-10-06 11:22:27 字数 3359 浏览 0 评论 0原文

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

问题是当您希望根据博客 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">>}]]

I have trouble sorting two related but separate lists of tuple lists. One list is made up of tuple lists representing a blog post. The other list is made up of tuple lists representing a comment post.

The problem is when you would like the same order based on blog id value. The lists for blog posts is sorted via the date value. So you cannot just sort numerically via blog id for both blog and comment post. And you cannot just sort the comment post via date value because the date values of blog and related comment post may be different.

I am not sure how to approach the problem - at least not in an elegant way.

Should I use lists:nth and consequently get each tuple list and position value? Then I would get the value of blog id, Then I would search in the list for comment posts for that id. Get the value of that tuple list. Associate the value of that tuple list in a new list with the appropriate nth position value.

Should I use the lists:sort function?

Any suggestions with code samples much appreciated.

Here are two sample lists of tuple lists that can be used as a basis :

[[{<<"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">>}]]

And the desired output is the following with first list unchanged and second list reordered :

[[{<<"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">>}]]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

死开点丶别碍眼 2024-10-13 11:22:27

好的,那么新的尝试:)

我们有:

-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,

此函数根据相关的 blog_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

Ok, new try then :)

We have:

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

Basic module exports to test the thing

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

Your definition of blogs.

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

Your definition of comments.

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

Your definition of being sorted.

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

Fetch all the blog_id values from the Blogs.

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

Number the order the blogs occur in. Stuff these into a dict for fast lookup later.

    %% 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,

This function compares two Comments, I, J to each other based on their related blog_id.

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

Return what we want to return.

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

Tester function.

2> c(foo).
{ok,foo}
3> foo:sort_test().
true
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文