Sort a nested structure in Coldfusion - coldfusion

I am trying to sort a nested struct using StructSort. Below is a simple example of what I am trying to do. The code below does not work and returns the following error "The specified element a does not contain a simple value." Is it possible to sort a nested structure in Coldfusion, and if so how?
<cfscript>
data = {"a":{"name":100},"b":{"name":50},"c":{"name":25},"d":{"name":75}};
dataSorted= StructSort(data, function(a,b) {
return compare(a.name, b.name);
});
writeDump(dataSorted);
</cfscript>
expected output
c
b
d
a
Also made a cffiddle here: https://cffiddle.org/app/e20a782a-be90-4e65-83de-e31eb83fdf4f

Docs: https://docs.lucee.org/reference/objects/struct/sort.html
<cfscript>
data = {
"a": {"name": 100},
"b": {"name": 50},
"c": {"name": 25},
"d": {"name": 75}
};
dataSorted = data.sort("numeric", "asc", "name")
writeDump(dataSorted);
</cfscript>
Result: array ["c", "b", "d", "a"]
This worked for me on lucee 5. something. The last argument in the sort function is the pathToSubElement within a struct. Fo your example it is simply one level deep using the name property.

Related

Using Postman How to write a test to check for duplicate ids

I would like to write a test in Postman that validates there are no duplicate values in the array of objects. Here is an example response:
This is my json response :
{
"content": [
{
"id": "88848990-c4c8-4e7d-b708-3e69e684085b",
"name": "UPDATED",
},
{
"id": "42c37e1d-eed3-4f5c-b76a-7b915c05b0bf",
"name": "Swoop ",
},
{
"id": "88848990-c4c8-4e7d-b708-3e69e684085b",
"name": "United Test Airlines",
},
I can see 2 ids that are the same I want to write a test in postman to identify any duplicates in my results. If the test picks up duplicates then it must fail, if it does not pick up any duplicates it should pass. Note I am new to postman api testing.
The short and esay solution might be:
Save all id in an array
Create a set from this array
Compare size of the array and the set. If it equals, then no duplication. If not, there is a duplication.
const res = pm.response.json();
const ids = _.map(res.content, "id");
pm.test("check duplicate id", () => {
const setIds = new Set(ids);
pm.expect(ids.length).eql(setIds.size);
})

How to perform a search in array of object flutter?

I am having a Map list with key and values, for example :
map<String, dynamic> my_List = [{"name": "mike", "age": "20"}, {"name":"william","age": "23"}].
I already tried containsValue but I don't want to use it.
The result i need to get is, when i search for m i need to get [{"name": "mike", "age": "20"}, {"name":"william","age": "23"}] , and when i search 3 i need the result as [{"name":"william","age": "23"}].
You could create a Person or User class as julemand101 has suggested but if You have to work with Map try this:
List<Map<String, dynamic>> search(String input){
return my_List.where((e) => e["name"].contains(input) || e["age"].contains(input)).toList();
}

How do I swap some characters of a String with values of a HashMap<String,String> in Kotlin?

Assuming I have a
val s: String = "14ABC5"
and have a HashMap
val b: HashMap<String,String> = hashMapOf("A" to "10", "B" to "11", "C" to "12", "D" to "13", "E" to "14", "F" to "15" )
How would I change all occurrences of A,B,C with 10, 11, 12 while keeping their order ("1", "4", "10", "11", "12", "5")?
So far I have this
val result: List<String> = s.toUpperCase().toCharArray().map{ it.toString() }.map{ it -> b.getValue(it)}
which works if ALL characters of the String exist in the HashMap but my String may contain inexistent keys as well.
You could either use getOrDefault(...), or the Kotlinesque b[it] ?: it.
By the way, if you're using the implicit lambda argument name (it), you can get rid of the it ->.
You can use the String as an iterable by default and simplify your code as follows:
s.map { it.toString() }
.map { b[it] ?: it }

autofilling a dict python 2.x

I'm quite new to Python and programming in general, so apologies if this is quite basic or has been asked and answered before. Here is a sample of the data I'm working with:
{
"homeTeam": {
"formation": [
"4",
"4",
"2"
],
"lineupsSorted": [
{
"player": {
"name": "Scott P. Brown",
"slug": "scott-p-brown",
"shortName": "S. P. Brown",
"id": 19889,
"hasImage": true
},
"position": 1,
"shirtNumber": 1,
"substitute": false,
"positionName": "Goalkeeper",
"positionNameshort": "G",
"captain": false
},
{
"player": {
"name": "Carl Winchester",
"slug": "carl-winchester",
"shortName": "C. Winchester",
"id": 110785,
"hasImage": true
},
"position": 2,
"shirtNumber": 27,
"substitute": false,
"positionName": "Midfielder",
"positionNameshort": "M",
"captain": false
},
I am looking to automate populating defined names as I have done manually here:
hometeamPositions =['Goalkeeper','Midfielder','Defender','Defender','Defender','Midfielder','Midfielder','Midfielder','Midfielder','Forward','Forward','Goalkeeper','Defender','Defender','Midfielder','Midfielder','Forward','Forward']
hometeamPlayers = ['S. P. Brown','C. Winchester','M. Onariase','W.
Boyle','J. Cranston','H. Pell','J. Rowe','K. Storer','B. Waters','D.
Wright','D. Holman','R. Lovett','J. Barthram','T. Plavotic','J.
Munns','L. Davis','K. Wootton','J. Dayton']
As I will be repeating this process many hundreds of times with different data (same structure) I was wondering if anyone could give me some tips on automatically building these ranges?
Thanks,
Peter
I'm not sure I understood what is the problem you are trying to solve but I'll try to help.
Assuming you have a dictionary team_dict and you want to create 2 list: hometeamPositions and hometeamPlayers you can use the following code:
hometeamPlayers = []
hometeamPositions = []
for player_dict in teams_dict['homeTeam']['lineupsSorted']:
hometeamPlayers.append(player_dict['player']['shortName'])
hometeamPositions.append(player_dict['positionName'])
The output on your example will be:
hometeamPlayers = ['S. P. Brown', 'C. Winchester']
hometeamPositions = ['Goalkeeper', 'Midfielder']

How to collate list items with the same first item onto a map

I know this is probably a very simple List operation in Scala, but I'm a newbie and can't figure it out. I have a query that returns a result set with a series of values, grouped by a common id. For example:
Result Set:
[{ 1, "a", 30 },
{ 1, "b", 20 },
{ 1, "c", 22 },
{ 2, "a", 32 },
{ 2, "c", 10 }]
and what I'd like to do is put this into a map as such:
1 -> [{"a", 30}, {"b", 20}, {"c", 22}]
2 -> [{"a", 32}, {"c", 10}]
I think the collect method can be used for this but can't figure it out.
I'm not sure what the types in your data structure are, but maybe you can adapt this. This assumes you have a collection of tuples:
val items =
List((1, "a", 30),
(1, "b", 20),
(1, "c", 22),
(2, "a", 32),
(2, "c", 10))
items
.groupBy{ case (a,b,c) => a }
.mapValues(_.map{ case (a,b,c) => (b,c) })
// Map(1 -> List((a,30), (b,20), (c,22)), 2 -> List((a,32), (c,10)))
Or, more succinctly:
items.groupBy(_._1).mapValues(_.map(t => (t._2, t._3)))
The collect method is something else entirely (basically, it's map that drops non-matching values). The groupBy method is what you were really looking for.