r - 如何按 AWS 调用的字节顺序对列表进行排序

标签 r sorting amazon-web-services amazon-product-api endianness

看看http://associates-amazon.s3.amazonaws.com/signed-requests/helper/index.html

以下名称-值对:

Service=AWSECommerceService
Version=2011-08-01
AssociateTag=PutYourAssociateTagHere
Operation=ItemSearch
SearchIndex=Books
Keywords=harry+potter
Timestamp=2015-09-26T14:10:56.000Z
AWSAccessKeyId=123

The name-value pairs have been sorted according to byte-order

应该导致

AWSAccessKeyId=123
AssociateTag=PutYourAssociateTagHere
Keywords=harry%20potter
Operation=ItemSearch
SearchIndex=Books
Service=AWSECommerceService
Timestamp=2015-09-26T14%3A10%3A56.000Z
Version=2011-08-01

如何在 R 中实现这一点?

据我所知,它们是按其排序的 as.numeric(charToRaw(name)) 值。如果第一个值相等,则按第二个值排序,然后是第三个值,依此类推。

问题:如何在 R 中执行此操作?

最佳答案

# Name-Value-Pairs
nvp <- list(                                
"Service"="AWSECommerceService",
"Version"="2011-08-01",
"AssociateTag"="PutYourAssociateTagHere",
"Operation"="ItemSearch",
"SearchIndex"="Books",
"Keywords"="harry potter",
"Timestamp"="2015-09-26T14:10:56.000Z",
"AWSAccessKeyId"="123"
)

获取字节:

bytes <- function(chr){
  as.data.frame(t(as.numeric(charToRaw(chr))))
}

计算字节,并绑定(bind)值

b <- lapply(names(nvp), bytes)
b <- data.table::rbindlist(b, fill=TRUE) # other than base::rbind, this fills by NA

按第一列对名称进行排序,然后按第二列、第三列……依此类推

names(nvp)[do.call(order, as.list(b))]

[1] "AWSAccessKeyId" "AssociateTag"   "Keywords"       "Operation"      "SearchIndex"   
[6] "Service"        "Timestamp"      "Version"   

最后nvp[do.call(order, as.list(b))] 返回正确排序的列表

关于r - 如何按 AWS 调用的字节顺序对列表进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32798526/

相关文章:

r - 如何在 R 中的变量中拆分字符串变量(非矩形)

javascript - 如何打印数组中变量的名称?

c - stdlib 的 qsort 是递归的吗?

amazon-web-services - 无法在 AWS 中创建/更新堆栈

r - 如何在barplot中手动放置错误行?

r - 在 purrr 的 map() 函数中使用 levene_test?

r - 使用 data.table 创建一列回归系数

python - 如何使用元组值的自定义比较器创建字典的排序表示?

amazon-web-services - 具有 2 个 AWS 账户的 AWS CodePipeline 最佳实践

amazon-web-services - 如何计算 CloudWatch 日志中 IP 地址的出现次数