Is it possible to sort a sequence of nested maps with Specter? The sequence example:
(
{:entity/id "1",
:entity/locations [
{:location/id "101",
:location/name "1 First",
:location/type :locationType/type1
}
{:location/id "102",
:location/name "1 Second",
:location/type :locationType/type2
}
{:location/id "103",
:location/name "1 Third",
:location/type :locationType/type3
}]}
{:entity/id "3",
:entity/locations [
{:location/id "301",
:location/name "3 First",
:location/type :locationType/type1
}
{:location/id "302",
:location/name "3 Second",
:location/type :locationType/type2
}
{:location/id "303",
:location/name "3 Third",
:location/type :locationType/type3
}]}
{:entity/id "2",
:entity/locations [
{:location/id "201",
:location/name "2 First",
:location/type :locationType/type1
}
{:location/id "202",
:location/name "2 Second",
:location/type :locationType/type2
}
{:location/id "203",
:location/name "2 Third",
:location/type :locationType/type3
}]}
)
I want to sort it by :location/name of the :entity/locations element where :location/type is some value (e.g. :locationType/type3)
So, if I sort it by :location/name where :locationType is :locationType/type3 I would get the sequence where the entities (`:entity/id`) would be in the following order: "1","2","3".There's a pr for this
well, kinda
You can do sort-by and use select as they keyfn
Ah, ok, I am doing that, but thought sorting might be possible via specter...
There's a PR to add a SORTED navigator which gives you a sorted view over the data, but it wouldn't do what you're after here I don't think.
You could also use transform and then use sort as the transform function.
@suskeyhose can you give me an example? I'm pretty new with specter...
(transform [ALL :entity/locations] (partial sort-by :location/name) your-data)
This would take each entity and sort its locations by their name.Oh wait
I think I mistook your meaning
You want to make something where the entities are sorted based on the :location/name of a location with a specific type.
Give me a couple minutes, I can devise something for that.
Okay, so to start off with, there's a question I have about the data. Are the locations vectors going to include only one location for each location type?
And if so, is this data shaped like this because it got back this way from an API? Is there any way you could change it to a map from location types to these maps, instead of a vector?
(whether or not you can doesn't change if it's possible, only what's easier)
I cannot change the structure, this is the map I have to work with
is there a guarantee that there will only be one location of a given type?
(sort-by #(select-one [:entity/locations ALL (selected? :location/type (pred= the-type-to-select)) :location/name] %) the-data)This is how I would do it