Skip to content

Instantly share code, notes, and snippets.

@moehuster
Created December 12, 2019 12:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moehuster/ad3d86ea728de6160fed849434aa7638 to your computer and use it in GitHub Desktop.
Save moehuster/ad3d86ea728de6160fed849434aa7638 to your computer and use it in GitHub Desktop.
sort_json by name
#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
#include <algorithm>
#include <iostream>
using namespace rapidjson;
int main () {
Document d;
d.Parse("{\"c\":3, \"b\":2, \"a\":1, \"d\":4}");
std::sort(d.MemberBegin(), d.MemberEnd(), [](const Value::Member& lhs, const Value::Member& rhs){
return std::strcmp(lhs.name.GetString(), rhs.name.GetString()) < 0; });
StringBuffer sb;
Writer<StringBuffer> writer(sb);
d.Accept(writer);
std::cout << sb.GetString() << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment