Arrays 在Ruby中按字母顺序对CSV文件排序

Arrays 在Ruby中按字母顺序对CSV文件排序,arrays,ruby,csv,sorting,Arrays,Ruby,Csv,Sorting,但是看到这将是多么的无效,因为一次只能对一行进行排序,我正在寻找另一种解决方案。按字母顺序对进入CSV的数据进行排序的另一种方法是什么?据我所见,我不太可能在循环中对CSV文件进行排序,但我不确定如何在循环之外进行排序。因此,如果您有任何建议,请与我们分享。首先对学生进行排序怎么样 row.sort_by! {|student| [student.last_name, student.firstname]} 但这仍然是错误的,因为您可能有太多的结果无法一次放入内存,因此您应该使用#find_

但是看到这将是多么的无效,因为一次只能对一行进行排序,我正在寻找另一种解决方案。按字母顺序对进入CSV的数据进行排序的另一种方法是什么?据我所见,我不太可能在循环中对CSV文件进行排序,但我不确定如何在循环之外进行排序。因此,如果您有任何建议,请与我们分享。

首先对学生进行排序怎么样

row.sort_by! {|student| [student.last_name, student.firstname]}

但这仍然是错误的,因为您可能有太多的结果无法一次放入内存,因此您应该使用
#find_each
,它将批量加载数据:

self.students
.includes(:events)
.order(last_name: :asc, first_name: :asc)
.each do |student|
    csv << [
      student.first_name,
      student.last_name,
      student.title,
      student.id,
    ]
end
self.students
.包括(:事件)
.订单(姓::asc,名::asc)
.找到每个学生|

csv首先对学生进行排序怎么样

row.sort_by! {|student| [student.last_name, student.firstname]}

但这仍然是错误的,因为您可能有太多的结果无法一次放入内存,因此您应该使用
#find_each
,它将批量加载数据:

self.students
.includes(:events)
.order(last_name: :asc, first_name: :asc)
.each do |student|
    csv << [
      student.first_name,
      student.last_name,
      student.title,
      student.id,
    ]
end
self.students
.包括(:事件)
.订单(姓::asc,名::asc)
.找到每个学生|

csv自我学生包括(:事件)
的结果是什么。我希望是一个可枚举的,那么您对的期望是错误的,因为一次只能对一行进行排序。你可以尝试
self.students.includes(:events).按{student.last\u name,student.firstname]}对你进行排序。每个…
非常感谢,这个解决方案成功了!
self.students.includes(:events)
的结果是什么。我希望是一个可枚举的,那么您对的期望是错误的,因为一次只能对一行进行排序。你可以尝试
self.students.includes(:events).按{student.last\u name,student.firstname]}对你进行排序。每个…
非常感谢,这个解决方案成功了!
self.students
.includes(:events)
.order(last_name: :asc, first_name: :asc)
.find_each do |student|
    csv << [
      student.first_name,
      student.last_name,
      student.title,
      student.id,
    ]
end
self.students
.includes(:events)
.order(last_name: :asc, first_name: :asc)
.select(:first_name, :last_name, :title, :id)
.find_each do |student|
    csv << [
      student.first_name,
      student.last_name,
      student.title,
      student.id,
    ]
end