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 8657953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:38:55+00:00 2026-06-12T15:38:55+00:00

I’m new to ruby and I’m trying to write a dijkstra function but my

  • 0

I’m new to ruby and I’m trying to write a dijkstra function but my hash sort seems doesn’t work at all

def distance(start_code, end_code, map)
#initialize hash for distance 
#distance are initialized to -1
dist_hash=Hash.new()
start_hash=Hash.new()
parent_hash=Hash.new()
close_list=Array.new()
find=-1
map.citylist.each do |e|
  dist_hash[e]=[+1.0/0.0]
end

start_hash[start_code]=0
parent_hash[start_code]=start_code

while (start_hash.empty?)==false


   #sort the hash
  start_hash.sort_by {|k,v| v}
  puts 'value'      
  puts start_hash.values()  
   #pop the first item in the hash
  h=start_hash.shift()
  curr_key=h[0]
  curr_val=h[1]
  curr_city=map.findcity(curr_key)
  close_list<<curr_city.code

   #for every one in adjacent list
  curr_city.get_adj_city().each do |e|


     #if it in the close list then igonore
    if close_list.include?(e)==false  
       #if it is not in the start_hash then add to start hash
      if start_hash.has_key?(e)==false
        dist=map.adj_dist(curr_city.code, e)
        dist=dist+curr_val
        start_hash[e]=dist
        parent_hash[e]=curr_city.code
       #if it is in the start_hash check if we have better distance
      else
        dist=map.adj_dist(curr_city.code, e)
        if (dist+curr_val)<start_hash[e]
          parent_hash[e]=curr_city.code
          start_hash[e]=dist
        end
      end
       #end pf checking single adj city
    end
     #end of check if include in close


  end
   #end of check whole list

  if curr_city.code==end_code
    find=0
    break
  end

end
#end of check node
#result
if find==0
  ptr=end_code
  puts ptr
  puts "final list"

  while ptr!=start_code
    ptr=parent_hash[ptr]
    puts ptr
  end
  return 0
else
  return -1
end

end

When I’m trying to call d.distance(“BUE”, “LOS”, map)

the output looks like

value
0
value
1680
4651
value
10053
8047
4651
value
11094
15839
15839
8047
4651
10779
....

the values are printed out right after hash.sort_by but not sorted. Am I using the method correctly?

  • 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-12T15:38:56+00:00Added an answer on June 12, 2026 at 3:38 pm

    the values are printed out right after hash.sort_by but not sorted. Am I using the method correctly?

    No. When I’m not sure how something works, I open up IRB and try a few things with it:

    hash = {a:1, b:2, c:4, d: 3}
    => {:a=>1, :b=>2, :c=>4, :d=>3}
    hash.sort
    => [[:a, 1], [:b, 2], [:c, 4], [:d, 3]]
    hash
    => {:a=>1, :b=>2, :c=>4, :d=>3}
    hash.sort_by{|k,v| v }
    => [[:a, 1], [:b, 2], [:d, 3], [:c, 4]]
    hash
    => {:a=>1, :b=>2, :c=>4, :d=>3}
    

    sort_by does not alter the hash, it returns a result. Try:

    hash = hash.sort_by{|k,v| v } # <- don’t use this, it’s an array and you’ll mislead anyone reading this code.

    sorted_tuples = hash.sort_by{|k,v| v }
    

    or something like it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want to construct a data frame in an Rcpp function, but when I
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I want use html5's new tag to play a wav file (currently only supported
I am trying to render a haml file in a javascript response like so:
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.