开发者

displaying data of a file in different sorted manner...How do that

I am trying to show data on a console screen. The data will be fetched from a text file. I have to sort that information according to the columns showing the hotel information

------------------------------------------------------------------
|     Hotel Name     |    Location   |   开发者_开发百科 Cuisines     |  Price  |
------------------------------------------------------------------
 Margarita            New York        Non veg-Veg          $15.32
 Burgundian           London          Veg-Non-veg          $10.50
 Krishna sagar        Bangalore       andhra style         $30.25
 Adan saga            Ayers           north special        $40.12
 Taj                  Hyderbad        Biryani special     $120.78
 Ajanta               Hyderbad        Andhra special       $45.02
 Elora                Yorkshire       Mutton special      $135.45
 Tad banjo            Maine           Chicken Biryani     $120.87
 Tao                  Huston          North special       $100.00
 Punjabi kabi         Spencer marker  Paratha Special       45.78
------------------------------------------------------------------

Some how, I managed the alignment, but that is not a proper way to display. I don't know how to do the sorting because, when I check the text file, every data-item is stored as a separate entity.

Here is my code:

module ListHotel
 :sortorder => "HotelName"
 def ListHotel::listing
 puts('Showing Hotel Information')
 getter
 #puts('[Shorting Order]:#{:sortorder}')

 end # end of the Method

  def self.getter
           arr=Array.new
           File.open('Restaurant.txt','r').each { |x| arr << x}
  1.upto 73  do  print('-') end 
  puts "\n" 
  puts %q{|      Hotel Name    |    Location    |     Cuisines |    Price      |}
  1.upto 73  do  print('-') end;puts "\n" 
   i=0
  while i<=arr.length
    print "#{arr[i].chomp rescue nil}\t\t" ;i+=1 
    print "#{arr[i].chomp rescue nil}\t" ;i+=1 
    print "#{arr[i].chomp rescue nil}\t\t" ;i+=1 
    print "#{arr[i].chomp rescue nil}\n" ;i+=1 
  end  #while Ends 
1.upto 73  do  print('-') end;puts "\n" 
     puts"Choose your sorting way"
     puts %q{ :By Hotel Name
              :By Location
              :By Cuisines
              :By Price
            }
 ch=gets.chomp rescue nil
#....
.....
.
....#

  end   #getter

end #end of Module


Edited following a comment from the questioner.

Further edited following a related question hereQuestion

The data that comes from the text is like this:

data = [
  "Margarita", "New York", "Non veg-Veg", 15.32,
  "Burgundian", "London", "Veg-Non-veg", 10.5,
  "Krishna sagar", "Bangalore", "andhra style", 30.25,
  "Adan saga", "Ayers", "north special", 40.12
]

You want to turn this into an array of array by doing this:

data = data.each_slice(4).to_a

The data now becomes:

data = [
  ["Margarita", "New York", "Non veg-Veg", 15.32],
  ["Burgundian", "London", "Veg-Non-veg", 10.5],
  ["Krishna sagar", "Bangalore", "andhra style", 30.25],
  ["Adan saga", "Ayers", "north special", 40.12]
]

If you want to sort by the price, considering that it may sometimes be nil:

data.sort_by{|hotel, location, cuisine, price| price.to_f}

# => [
  ["Burgundian", "London", "Veg-Non-veg", 10.5],
  ["Margarita", "New York", "Non veg-Veg", 15.32],
  ["Krishna sagar", "Bangalore", "andhra style", 30.25],
  ["Adan saga", "Ayers", "north special", 40.12]
]

If you want to sort by the location, then by the price, use an array:

data.sort_by{|hotel, location, cuisine, price| [location.to_s, price.to_f]}

# => [
  ["Adan saga", "Ayers", "north special", 40.12],
  ["Krishna sagar", "Bangalore", "andhra style", 30.25],
  ["Burgundian", "London", "Veg-Non-veg", 10.5],
  ["Margarita", "New York", "Non veg-Veg", 15.32]
]

Good that you can align them into a table.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜