July 22

I'm trying to convince the library's sort() to order an array of JSON objects, where the "name" field is the key for the sort. The following is the closest I've come, but it's not very close!

TIA (as always),
Andy

import std.json : JSONValue;
import std.algorithm.sorting : sort;

void main() {
    JSONValue[] vals;

    auto j1 = JSONValue.emptyObject;
    j1.object["name"] = "Sam";
    vals ~= j1;
    auto j2 = JSONValue.emptyObject;
    j2.object["name"] = "Joe";
    vals ~= j2;

    alias myComp = (x, y) => (x.object["name"] < y.object["name"]);
    vals.sort!(myComp);
    writeln(vals);
}
July 22

On Tuesday, 22 July 2025 at 21:24:04 UTC, Andy Valencia wrote:

>

I'm trying to convince the library's sort() to order an array of JSON objects, where the "name" field is the key for the sort. The following is the closest I've come, but it's not very close!

Closer than I thought. I just had to add .str to each element, then the comparison worked and sort() did its job.

Sorry for the noise.
Andy