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

  • Home
  • SEARCH
  • 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 8917827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:35:23+00:00 2026-06-15T05:35:23+00:00

mydata = {:data => [ { :first_name = > abc, :foo_id = > 21sd,

  • 0
mydata = {:data => [
        {
        :first_name = > "abc",
        :foo_id = > "21sd",
        :roll = > 43
        }, 
        {:first_name = > "def",
        :foo_id = > "2uf",
        :roll = > 81
        }, 
        {:first_name = > "xyz",
        :foo_id = > "ac32",
        :roll = > 2
        }
    ]
}

In mydata :roll has somehow corrupted value

I have a sorted hash, this has data as :foo_id => :roll

sorted = {"21sd" => 7, "ac32" => 89, "2uf" => 92}

(that is by sorted ascending correct value of :roll)

And I want to use this ‘sorted’ hash to rearrange ‘mydata’ and also over-ride the value of :roll in ‘mydata’ with the correct value from ‘sorted’ hash. So finally ‘mydata’ will look like

mydata = {:data => [
        {
        :first_name = > "abc",
        :foo_id = > "21sd",
        :roll = > 7
        }, 
        {:first_name = > "xyz",
        :foo_id = > "ac32",
        :roll = > 89
        },
        {:first_name = > "def",
        :foo_id = > "2uf",
        :roll = > 92
        }
    ]
}

UPADTE: :roll may not be unique in sorted

Consider mydata may have 100,000 hashes

I have achieved the result using nested loop of ‘sorted’ and in each iteration, searching the foo_id from ‘mydata’ and correcting the value and stroring the sorted data in new variable. Which is ugly.

correct = []
sorted.each {|k, v|
    mydata[:data].each {|h| # hate looping here
        if h[:foo_id] == k  # hate searching here, if i have 100,000 record in 'mydata'
            h[:roll] = v
           correct << h 
        end
    }
}
mydata = {:data => correct}

This is not an optimal solution if mydata contains large numbers of data sets. Anyone suggest some optimal solution?

  • 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-06-15T05:35:24+00:00Added an answer on June 15, 2026 at 5:35 am

    This should work…

    mydata[:data].each do |h|
      h[:roll] = sorted[h[:foo_id]]
    end
    

    There’s no way to avoid looping through mydata[:data] though as it’s an Array… but you don’t need to stuff things into a new variable.. just update what you’ve got.

    Unless I’m not understanding the problem…

    EDIT: I wasn’t picking up the sorting right. New solution:

    mydata[:data].each do |h|
      h[:roll] = sorted[h[:foo_id]]
    end
    mydata[:data].sort_by!{|h| h[:roll]}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My data has these properties: Each entry has a unique id (Id) Each has
Let say if I have this data my_data = [ 10 20 30 40;
My data is just number, I want pass it from VC1 to VC2 for
My data set contains a column named IconUri that contains a full HTTP address
My data in Oracle is something like this NAME | DEP_VALUE | ID_DEP Amy
My data looks like so: TEST 2012-05-01 00:00:00.203 OFF 0 2012-05-01 00:00:11.203 OFF 0
My data in db is stored in html format along with image tags in
My data is tab delimited and looks like this: Name Count Sample Dog .0001
My data looks like the following: id|category|insertdate|title.... -------------------------------- 1|1|123|test 1 2|1|124|test 2 3|1|125|test 3
My data looks as follows MyText ------- some text, some more text, even more

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.