specter

Kris C 2022-02-21T11:18:51.339179Z

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".

2022-02-21T23:28:33.004109Z

There's a pr for this

2022-02-21T23:28:42.808179Z

well, kinda

2022-02-21T23:29:29.642179Z

You can do sort-by and use select as they keyfn

Kris C 2022-02-22T07:48:03.481639Z

Ah, ok, I am doing that, but thought sorting might be possible via specter...

2022-02-22T15:47:03.263569Z

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.

2022-02-22T15:47:16.861179Z

You could also use transform and then use sort as the transform function.

Kris C 2022-02-22T16:31:53.710979Z

@suskeyhose can you give me an example? I'm pretty new with specter...

2022-02-22T16:33:36.152979Z

(transform [ALL :entity/locations] (partial sort-by :location/name) your-data)
This would take each entity and sort its locations by their name.

2022-02-22T16:34:01.878519Z

Oh wait

2022-02-22T16:34:07.529459Z

I think I mistook your meaning

2022-02-22T16:34:41.269249Z

You want to make something where the entities are sorted based on the :location/name of a location with a specific type.

✅ 1
2022-02-22T16:34:47.455139Z

Give me a couple minutes, I can devise something for that.

2022-02-22T16:36:16.191699Z

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?

2022-02-22T16:36:45.461519Z

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?

2022-02-22T16:37:02.864779Z

(whether or not you can doesn't change if it's possible, only what's easier)

Kris C 2022-02-22T16:38:33.366639Z

I cannot change the structure, this is the map I have to work with

2022-02-22T16:38:50.465349Z

is there a guarantee that there will only be one location of a given type?

2022-02-22T16:45:58.884769Z

(sort-by #(select-one [:entity/locations ALL (selected? :location/type (pred= the-type-to-select)) :location/name] %) the-data)

2022-02-22T16:46:03.512009Z

This is how I would do it