以不同的排序方式显示文件的数据......怎么做

时间:2011-04-13 12:06:57

标签: ruby sorting

我正在尝试在控制台屏幕上显示数据。数据将从文本文件中获取。我必须根据显示酒店信息的栏目对信息进行排序

------------------------------------------------------------------
|     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
------------------------------------------------------------------

一些如何,我管理了对齐,但这不是一种正确的显示方式。我不知道如何进行排序,因为当我检查文本文件时,每个数据项都存储为一个单独的实体。

这是我的代码:

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

1 个答案:

答案 0 :(得分:1)

根据提问者的评论进行编辑。

在此处根据相关问题进行了进一步修改 Question

来自文本的数据如下:

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
]

你想通过这样做把它变成一个数组数组:

data = data.each_slice(4).to_a

现在数据变为:

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]
]

如果您想按价格排序,请考虑有时可能是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]
]

如果您想按位置排序,那么按价格排序,请使用数组:

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]
]

很好,您可以将它们对齐到表格中。