javascript - map 究竟如何排序键?

标签 javascript c++ sorting dictionary key

我在 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/

相关文章:

javascript - 如何从 Angular 中的另一个 Controller 内的 Controller 访问对象

javascript - 如何在 Bootstrap Datepicker 中设置 maxViewMode (使用 JS Fiddle)

C++ - 将数据复制到 2 个不同线程的最快/可靠方法

c++ - Ruby C++ 风格的迭代器

javascript - Angular - 使用自定义方法扩展 $resource 子对象

javascript - 检查一个对象是否是谷歌桌面上的一个元素

c++ - C++ HTTPS 库在性能(例如内存占用、CPU 时间等)方面如何比较?

java - java对不同类的对象进行排序

sorting - pymongo sort() limit() 不同吗?

java - 数组排序降序