如何在Ruby(而不是Rails)中排序日期?

时间:2021-10-31 15:59:13

I know that Rails has sorting methods built into ActiveRecord, but I am just writing a plain-old ruby script and would love to sort the records from an array by date.

我知道Rails有ActiveRecord内置的排序方法,但我只是编写一个普通的ruby脚本,并希望按日期对数组中的记录进行排序。

The date would be stored in one of the cells of the multi-dimensional array.

日期将存储在多维数组的一个单元格中。

What's the best way for me to approach this, so I can get to the point where I just do sort_by_date and I indicate either ASC or DESC?

对我来说最好的方法是什么,所以我可以达到sort_by_date的地步,我指的是ASC或DESC?

I don't have to use the method sort_by_date, but the idea is I would like to be able to easily call a method on the collection and get the results I want.

我没有必要使用sort_by_date方法,但我的想法是,我希望能够轻松地调用集合上的方法并获得我想要的结果。

Thoughts?

4 个解决方案

#1


10  

Something like this?

像这样的东西?

  class Array
    def sort_by_date(direction="ASC")
      if direction == "ASC"
        self.sort
      elsif direction == "DESC"
        self.sort {|a,b| b <=> a}
      else
        raise "Invalid direction. Specify either ASC or DESC."    
      end
    end
  end

A multi-dimensional array is just an array of arrays, so call this method on the 'dimension' you want to sort.

多维数组只是一个数组数组,因此请在要排序的“维度”上调用此方法。

#2


12  

def sort_by_date(dates, direction="ASC")
  sorted = dates.sort
  sorted.reverse! if direction == "DESC"
  sorted
end

#3


3  

The way I am doing it right now it is:

我现在这样做的方式是:

@collection.sort! { |a,b|  DateTime.parse(a['date']) <=> DateTime.parse(b['date']) }

And with the ! operator I am affecting the same variable (otherwise I will need another one to hold the modified variable). So far, it's working as a charm.

随着!运算符我正在影响同一个变量(否则我需要另一个来保存修改后的变量)。到目前为止,它正在成为一种魅力。

#4


2  

The following might not work for Date object, but should work for DateTime objects. This is because DateTime objects can be converted into an integer.

以下内容可能不适用于Date对象,但应适用于DateTime对象。这是因为DateTime对象可以转换为整数。

I recently had to do a descending sort for DateTime objects and this is what I came up with.

我最近不得不为DateTime对象做一个降序排序,这就是我提出的。

def sort_by_datetime(dates, direction="ASC")
  dates.sort_by { |date| direction == "DESC" ? -date.to_i : date }
end

#1


10  

Something like this?

像这样的东西?

  class Array
    def sort_by_date(direction="ASC")
      if direction == "ASC"
        self.sort
      elsif direction == "DESC"
        self.sort {|a,b| b <=> a}
      else
        raise "Invalid direction. Specify either ASC or DESC."    
      end
    end
  end

A multi-dimensional array is just an array of arrays, so call this method on the 'dimension' you want to sort.

多维数组只是一个数组数组,因此请在要排序的“维度”上调用此方法。

#2


12  

def sort_by_date(dates, direction="ASC")
  sorted = dates.sort
  sorted.reverse! if direction == "DESC"
  sorted
end

#3


3  

The way I am doing it right now it is:

我现在这样做的方式是:

@collection.sort! { |a,b|  DateTime.parse(a['date']) <=> DateTime.parse(b['date']) }

And with the ! operator I am affecting the same variable (otherwise I will need another one to hold the modified variable). So far, it's working as a charm.

随着!运算符我正在影响同一个变量(否则我需要另一个来保存修改后的变量)。到目前为止,它正在成为一种魅力。

#4


2  

The following might not work for Date object, but should work for DateTime objects. This is because DateTime objects can be converted into an integer.

以下内容可能不适用于Date对象,但应适用于DateTime对象。这是因为DateTime对象可以转换为整数。

I recently had to do a descending sort for DateTime objects and this is what I came up with.

我最近不得不为DateTime对象做一个降序排序,这就是我提出的。

def sort_by_datetime(dates, direction="ASC")
  dates.sort_by { |date| direction == "DESC" ? -date.to_i : date }
end