title: Sorting a hash table and printing key and value at the same time
body: <p>I have written a program in python, where I have used a hash table to read data from a file and then add data in the last column of the file corresponding to the values in the 2nd column of the file. for example, for all entries in column 2 with same values, the corresponding last column values will be added. </p>
  
  <p>Now I have implemented the above successfully.  Now I want to sort the table in descending order according to last column values and print these values and the corresponding 2nd column (key) values. i am not able to figure out on how to do this. Can anyone please help ?</p>
  
  <p>pmt txt file is of the form</p>
  
  <pre><code>0.418705 2 3 1985 20 0
  0.420657 4 5 119 3849 5 
  0.430000 2 3 1985 20 500
  </code></pre>
  
  <p>and so on...</p>
  
  <p>So, for example, for number 2 in column 2, i have added all data of last column corresponding to all numbers '2' in the 2nd column. So, this process will continue for the next set of numbers lie 4, 5 ,etc in column 2.</p>
  
  <p>I'm using python 3</p>
  
  <pre><code>import math
  
  source_ip = {}
  f = open("pmt.txt","r",1)
  lines = f.readlines()
  
  for line in lines:
      s_ip = line.split()[1]
      bit_rate = int(line.split()[-1]) + 40
      if s_ip in source_ip.keys():
         source_ip[s_ip] = source_ip[s_ip] + bit_rate
         print (source_ip[s_ip])
      else:
         source_ip[s_ip] = bit_rate
  
  f.close()
  
  for k in source_ip.keys():
      print(str(k)+": "+str(source_ip[k]))
      print ("-----------")
  </code></pre>