Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3485644
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T10:55:56+00:00 2026-05-18T10:55:56+00:00

I have trouble sorting two related but separate lists of tuple lists. One list

  • 0

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">>}]]
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T10:55:57+00:00Added an answer on May 18, 2026 at 10:55 am

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm migrating to Nhibernate 2.0 GA but have some trouble with setting cache expirations
I am developing a small windows app, but have some trouble deciding whether to
Conceptually, I would like to accomplish the following but have had trouble understand how
Conceptually, I would like to accomplish the following but have had trouble understand how
I try to use templatetags in django but I have trouble. I defined enumhelper.py
I have trouble comparing 2 double in Excel VBA suppose that I have the
I have trouble with the following piece of code. When I go through with
I have trouble using Perl grep() with a string that may contain chars that
I have trouble defining a trigger for a MySQL database. I want to change
I have no trouble building 1.35.0, as well as 1.36.0 on the timesys arm-gcc

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.