skip to Main Content

I have this JSON result of tweets from Twitter search API, ordered by created_at in descending order. What is the simplest way to reverse this JSON order so that they would be in ascending order instead?

{
   "data" : [
      {
         "created_at" : "2021-04-04T14:21:20.000Z",
         "id" : "1378714305974132736",
         "conversation_id" : "1378707256598990850",
         "in_reply_to_user_id" : "1036922707961626624",
         "text" : "But my Mom? my Mom? I will give that woman the world, and heaven will fall before I let her down. HEAVEN WILL FALL!!!! .nnI pray for everyone that is sick. May God heal you and provide resources 🙏 nn "HAPPY SURVIVAL ANNIVERSARY TO ME"nWHAT GOD CANNOT DO, DOES NOT EXIST!!!!",
         "author_id" : "1036922707961626624"
      },
      {
         "text" : "the sickness, and I feel bad about it sometimes.  But I know God will soon open financial doors for me. I'm grateful to my brothers, sisters, cousins, aunties, uncles, in-laws, friends, for being there for me in those dark times. Their Love kept me pushing.",
         "author_id" : "1036922707961626624",
         "id" : "1378714303755325444",
         "created_at" : "2021-04-04T14:21:19.000Z",
         "in_reply_to_user_id" : "1036922707961626624",
         "conversation_id" : "1378707256598990850"
      },
      {
         "conversation_id" : "1378707256598990850",
         "in_reply_to_user_id" : "1036922707961626624",
         "created_at" : "2021-04-04T14:21:19.000Z",
         "id" : "1378714301637263360",
         "author_id" : "1036922707961626624",
         "text" : "hurts sometimes. I don too suffer o. I just want to see where to make money and live a soft life. I deserve it, especially since I can no longer do things that are too tedious. And to complete my mother's house. She hasn't completed her house because of the millions she spent on"
      },
      {
         "text" : "So plix, I'm accepting birthday gifts 🌚🥺. This thread may be long but it is only a glimpse of what I went through. What I have narrated doesn't even give out one tenth of what I went through between 2017 and 2019, but I am most grateful to God. I am good now but my scars still",
         "author_id" : "1036922707961626624",
         "created_at" : "2021-04-04T14:21:18.000Z",
         "id" : "1378714299590447110",
         "conversation_id" : "1378707256598990850",
         "in_reply_to_user_id" : "1036922707961626624"
      },
      {
         "id" : "1378714297463934982",
         "created_at" : "2021-04-04T14:21:18.000Z",
         "in_reply_to_user_id" : "1036922707961626624",
         "conversation_id" : "1378707256598990850",
         "text" : "Like my mom said, I was brought back to life on the ressurection day. I came back from the dead with him. He brought me back to my family. He wiped the tears from my mom's eyes. Easter Sundays will forever be special to me and well, I'm taking it as my second birthday,",
         "author_id" : "1036922707961626624"
      },
      {
         "text" : "to 2019, I had so many blood transfusions.  I have like 50 people's blood in me, if not moreâ¥ï¸. I remember a time I even had partial amnesia. Guyyyy!!! I no dey remember anything again 😂. Why am I posting this today? My Sixth and final surgery was on Easter Sunday, 2019.",
         "author_id" : "1036922707961626624",
         "id" : "1378714295278649346",
         "created_at" : "2021-04-04T14:21:17.000Z",
         "in_reply_to_user_id" : "1036922707961626624",
         "conversation_id" : "1378707256598990850"
      },
      {
         "author_id" : "1036922707961626624",
         "text" : "In bits, but I could eat. My doctors said I was a miracle. They said the hospital would never forget me. They had never seen anything like what they saw in my tummy that day and they still couldn't believe I survived but I did. I survived. 6 times, I survived. And Omo, from 2017",
         "conversation_id" : "1378707256598990850",
         "in_reply_to_user_id" : "1036922707961626624",
         "created_at" : "2021-04-04T14:21:17.000Z",
         "id" : "1378714293382873090"
      },
      {
         "id" : "1378711697721061376",
         "created_at" : "2021-04-04T14:10:58.000Z",
         "in_reply_to_user_id" : "1036922707961626624",
         "conversation_id" : "1378707256598990850",
         "text" : "Long story short, I got better. I was healed. Everything had ended. I was free.  I was alive. The only thing I felt was excruciating pain which was normal. The fistula was gone, poo only came out of my anus, the bloating was gone, the obstruction was gone. I could eat now.",
         "author_id" : "1036922707961626624"
      },
      {
         "id" : "1378711694353035265",
         "created_at" : "2021-04-04T14:10:57.000Z",
         "in_reply_to_user_id" : "1036922707961626624",
         "conversation_id" : "1378707256598990850",
         "text" : "through the night. The doctors said it was risky to let me sleep that first night.  When it was dawn and I was still alive, my doctors were happy. Then the maintenance process began, to make sure my outer wounds healed and that the fistula didn't return and it did NOT return.",
         "author_id" : "1036922707961626624"
      },
      {
         "in_reply_to_user_id" : "1036922707961626624",
         "conversation_id" : "1378707256598990850",
         "id" : "1378711690729160704",
         "created_at" : "2021-04-04T14:10:56.000Z",
         "author_id" : "1036922707961626624",
         "text" : "I did, my brother came to me,  held my hand real tight and kissed my forehead. I could feel how fast his heart was beating, he had been so scared. My mom couldn't contain her joy. She just kept smiling at me. My mom and sister had to take turns that night to keep me awake all"
      }
   ],
   "meta" : {
      "result_count" : 10,
      "oldest_id" : "1378711690729160704",
      "newest_id" : "1378714305974132736",
      "next_token" : "-"
   }
}

I know that I could loop through these JSON objects, convert the created_at to datetime, put the each object in array, then sort the array by created_at column. But it seems very inefficient, especially if the JSON is large. So what is the best solution?

2

Answers


  1. Chosen as BEST ANSWER

    Solved it. I could just use .reverse on the data


  2. Just in case someone else is wondering, if sorting by another attribute/key is needed:

    data.sort_by { |hash| hash['key'] }
    

    or

    data.sort_by { |hash| hash['key'] }.reverse
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search