我在 javascript 中对 json 进行数字签名,以通过 C++ 进行验证。我正在使用 json spirit解析将 map
用于 json Object
的 json,因此 Object
需要按照与 c++ map
。
An Object
can easily be sorted by javascript
function sortObject(o) {
var sorted = {},
key, a = [];
for (key in o) {
if (o.hasOwnProperty(key)) {
a.push(key);
}
}
a.sort();
for (key = 0; key < a.length; key++) {
sorted[a[key]] = o[a[key]];
}
return sorted;
}
但是,我不确定 exact sorting method that c++ uses .
上面的代码是否足以让 javascript 以与 map
键排序相同的方式对 Object
键进行排序?如果没有,怎么办?
最佳答案
你基本上是在问 this问题。正如最佳答案所述:
Yes, that's [the iteration order is] guaranteed. Moreover, *begin() gives you the smallest and *rbegin() the largest element, as determined by the comparison operator, and two key values a and b for which the expression !compare(a,b) && !compare(b,a) is true are considered equal. The default comparison function is std::less.
关于javascript - map 究竟如何排序键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23017379/