Erlang: sort or order function for a list of tuple lists

I have a problem sorting two linked but separate lists of tuple lists. One list consists of lists of tuples representing a blog post. Another list consists of lists of tuples representing the comment.

The problem is that you need the same order based on the blog id value . Blog post lists are sorted by date value. Thus, you cannot just sort numerically using the id id of the blog for the blog and comment. And you can't just sort the comment after the date value , because the blog date values ​​and the related comment may be different.

I'm not sure how to approach the problem - at least not elegantly.

Should I use lists: nth and therefore get each list of tuples and position value? Then I get the blog id value, Then I would search the comments list for posts for this id. Get the value of this list of tuples. Associate the value of this list of tuples in the new list with the corresponding value of the nth position.

Should I use lists: sort a function?

Any suggestions with code examples were appreciated.

Here are two examples 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 result is as follows: the first list does not change and the second list is 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">>}]]
+3
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">>}]].

.

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,

, I, J 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
+3

Source: https://habr.com/ru/post/1779022/


All Articles