Sorting arrays of objects, for instance JSONValue's?

Andy Valencia dont at spam.me
Tue Jul 22 21:24:04 UTC 2025


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

```d
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);
}
```



More information about the Digitalmars-d-learn mailing list