可对元素为Hash型的数组排序的Ruby Gem: sort_collections

9 篇文章 0 订阅
7 篇文章 0 订阅

sort_collections:https://github.com/lanrion/sort_collections

 

这个gem的目的是,当我们通过API接收数据的时候(没有使用ActiveRecord),会出现需要对当前数组进行排序,Ruby中已经提供了sort方法,但是该方法,只是对简单的String、Integer等类型排序,如果是Hash呢?我需要针对Hash里的某个key进行排序呢? 这个 sort_collections:https://github.com/lanrion/sort_collections 就是解决这个问题的。

 

安装

gem 'sort_collections'

 

使用举例

 

# init sort data
hash_array = [{"created_at"=>"Fri, 31 May 2013 00:26:53 HKT +08:00",
              "email"=>"peop@example.edu",
              "updated_at"=>"Fri, 31 May 2013 00:27:21 HKT +08:00"},
             {"created_at"=>"Thu, 30 May 2013 18:53:12 HKT +08:00",
              "email"=>"coco@example.com",
              "updated_at"=>"Sat, 06 Jul 2013 17:25:13 HKT +08:00"},
             {"created_at"=>"Sun, 16 Jun 2013 11:23:42 HKT +08:00",
              "email"=>"lele@example.com",
              "updated_at"=>"Tue, 23 Jul 2013 15:44:46 HKT +08:00"},
             {"created_at"=>"Fri, 31 May 2013 11:05:24 HKT +08:00",
              "email"=>"bebe@example.com",
              "updated_at"=>"Tue, 06 Aug 2013 13:19:09 HKT +08:00"},
              {"created_at"=>"Fri, 31 May 2013 11:05:24 HKT +08:00",
              "email"=>"bebe@example.com",
              "updated_at"=>"Tue, 06 Aug 2013 13:19:09 HKT +08:00"},
             {"created_at"=>"Thu, 30 May 2013 11:19:11 HKT +08:00",
              "email"=>"admin@example.com",
              "updated_at"=>"Wed, 11 Sep 2013 15:13:25 HKT +08:00"}]

# default: order_type: "asc", order_with: "id"
hash_array.order_collection

# assign order_with, e.g.: "email"
hash_array.order_collection(order_with: "email")

# ==> Result:

[{"created_at"=>"Thu, 30 May 2013 11:19:11 HKT +08:00",
  "email"=>"admin@example.com",
  "updated_at"=>"Wed, 11 Sep 2013 15:13:25 HKT +08:00"},
 {"created_at"=>"Fri, 31 May 2013 11:05:24 HKT +08:00",
  "email"=>"bebe@example.com",
  "updated_at"=>"Tue, 06 Aug 2013 13:19:09 HKT +08:00"},
 {"created_at"=>"Fri, 31 May 2013 11:05:24 HKT +08:00",
  "email"=>"bebe@example.com",
  "updated_at"=>"Tue, 06 Aug 2013 13:19:09 HKT +08:00"},
 {"created_at"=>"Thu, 30 May 2013 18:53:12 HKT +08:00",
  "email"=>"coco@example.com",
  "updated_at"=>"Sat, 06 Jul 2013 17:25:13 HKT +08:00"},
 {"created_at"=>"Sun, 16 Jun 2013 11:23:42 HKT +08:00",
  "email"=>"lele@example.com",
  "updated_at"=>"Tue, 23 Jul 2013 15:44:46 HKT +08:00"},
 {"created_at"=>"Fri, 31 May 2013 00:26:53 HKT +08:00",
  "email"=>"peop@example.edu",
  "updated_at"=>"Fri, 31 May 2013 00:27:21 HKT +08:00"}]

 

这样子就非常方便。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这个错误是因为编译器无法确定 `std::hash<std::string>::operator()` 函数的返回值是否是编译期常量,因此不能在 `constexpr` 上下文中使用该函数。 解决方法是使用 `constexpr` 关键字显式地声明 `std::hash<std::string>::operator()` 函数是一个 `constexpr` 函数。示例代码如下: ```c++ #include <iostream> #include <functional> #include <string> using namespace std; // 显式声明 std::hash<std::string>::operator() 是一个 constexpr 函数 namespace std { template<> struct hash<string> { constexpr size_t operator()(const string& str) const { return std::_Hash_impl::hash(str.data(), str.length()); } }; } int main() { unordered_map<size_t, string> map = { {hash<string>()("hello"), "Hello World!"}, {hash<string>()("world"), "World Hello!"}, {hash<string>()("good"), "Good Morning!"}, {hash<string>()("night"), "Good Night!"} }; string str; cout << "Please enter a string: "; getline(cin, str); switch (hash<string>()(str)) { case hash<string>()("hello"): cout << map[hash<string>()("hello")] << endl; break; case hash<string>()("world"): cout << map[hash<string>()("world")] << endl; break; case hash<string>()("good"): cout << map[hash<string>()("good")] << endl; break; case hash<string>()("night"): cout << map[hash<string>()("night")] << endl; break; default: cout << "Unknown input!" << endl; break; } return 0; } ``` 在上面的代码中,我们显式声明了 `std::hash<std::string>::operator()` 函数是一个 `constexpr` 函数,编译器就可以确定该函数的返回值是编译期常量,从而避免了上述编译错误。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值