Sort array by difference from the given value - swift

I am trying to sort an array of football players - "players". The array consists of players and a single player has the following structure:
var player1 = ["name" : "Joe Smith",
"height": "42.00",
"experience": "yes",
"guardian" : "Jim and Jan Smith"]
The sorting that I would like should be based on the difference in player's height from their average height. So, the average height is 42.4. The player whose height is very close to the average height should be the first one after sorting, and the player whose height is really different from the average should be the last one.
As I understand, it is supposed to follow the following logic:
for player in players {
temp value = averageHeight - player["height"]
}
and then you compare value to values that have been already sorted and insert it in the correct position, however, this is my problem. I have problems writing this algorithm. Looking for any thoughts or pseudocode that would help resolve this issue. Thank you.

I strongly recommend that you convert your data to structs. This would allow you to do these kinds of calculations easily and without having to deal with string conversions all the time.
struct Player {
let name: String
let height: Double
}
var players = [
Player(name: "Foo", height: 42),
Player(name: "Bar", height: 1),
Player(name: "Baz", height: 100)
]
let avgHeight = players.reduce(0.0) { $0 + $1.height } / Double(players.count)
print(avgHeight)
players.sort { player1, player2 in
return abs(player1.height - avgHeight) < abs(player2.height - avgHeight)
}
print(players)

I think you are missing an abs(). Because sorting by averageHeight - player["height"] does not differ from sorting by player["height"] backwards. But sorting by abs(averageHeight - player["height"]) would do what you describe.
Then you can go for sort(by:):
players.sort(by:{abs(averageHeight-$0.height)<abs(averageHeight-$1.height)})
At least that is what I think, but I do not know Swift (ok, I have one from Suzuki)

Related

AG Grid Row-Grouping for showing version history of data

I'm a first time user of AG Grid and need some help identifying how to configure Row Grouping for my AG Grid in such a way that allows me to see my data group as a timeline/audit history. Was reading through the docs and haven't found an example on there that resembles what I'm looking for.
I have rowData that contains history and the visual expectation is that, the same base columnDefs assigned to the grid are used for the main "group" row (that contains the expand/collapse chevron) and all the expanded rows as well -- so there is no need for a "group column"
Anyone know how I can achieve this outcome? I have also looked at treeData and masterDetail but those didn't work out for me.
P.S. I don't rule out the possibility that I could be misreading/misunderstanding the AG Grid docs so any help there is appreciated.
EDIT: After fiddling around with things further, I believe I'm looking for a combination of two things:
The isRowMaster to enable expand/collapse on the main parent entry.
The groupDisplayType="groupRows" setting (minus the default row group cell because that would be handled by my master row from Point 1 instead)
After much head banging, turns out the solution was to use treeData. What I ended up creating was treeData containing only one level children.
Parent Node (expand/collapse)
child 1
...
child n
The other key was understanding how to structure your rowData and returning the proper dataPath to feed into the getDataPath handler for treeData. AG Grid expects a flat-list for rowData -- I say this as I was not able to get things working when having a nested tree structure like the following.
const mainRecord = {
id: "abc"
foo: 1,
bar: 2,
history: [
{ foo: 3, bar: 4 },
{ foo: 5, bar: 6 }
]
}
I had to modify my data structure above to return the history items as a flat list alongside the main record that I eventually sent as rowData. The other thing I had to do was add some hierarchy property to my objects to denote its placement in the tree. The hierarchy value is what I supplied to getDataPath
const rowData = [
{ id: "abc", foo: 1, bar: 2, hierarchy: ["abc"] }, // parent
{ foo: 3, bar: 4, hierarchy: ["abc", "34"] }, // child 1
{ foo: 5, bar: 6, hierarchy: ["abc", "56"] }, // child 2
]
After doing the above changes, I was able to get the expected visual outcome I was looking for. I think the big takeaway I got from this is as a first time user of AG Grid is, let it take care of grouping of your data on your behalf (as opposed to doing it yourself via structuring your models with some internal nested children structure)

Swift Checklist - Saving the Switch Data for multiple Rows

So I have a Simple UITableView that will display a UILabel and a UISwitch. I need to be able to get the values of each switch update my object then eventually send the data the API. I has a semi working concept. But this only worked if ALL the cells were visible. Before I only had 5 items to be checked. So I could simply loop through everything and get the data. Now my checklist has grown to over 20 items.
I understand to a degree why the current code doesn't work. It finds nil values. That would be the cells that aren't visible.
My big question is how do i go about capturing the value of all the cells and the value of all the UISwitch values and update my object?
I tried to use the KVO method. This is what I have so far, I have never used this before and a bit lost:
private var observation: NSKeyValueObservation?
observation = switchCell.observe(\.switchOne, options: [.old, .new]) { value, change in
print("value: \(value.switchOne.isOn)")
print("change old: \(change.oldValue)")
print("change new: \(change.newValue)")
}
My issue is I am not sure what I am supposed to use for the key path. Iget the following warning:
Passing reference to non-'#objc dynamic' property 'switchOne' to KVO method 'observe(_:options:changeHandler:)' may lead to unexpected behavior or runtime trap
Then the simulator doesn't boot up. What is the easiest way/best way to do this? Like I said I have 2)+ items on my checklist as I completed each item, I turn the switch on. Then I need to get the value for each item, and update the object with the correct value. Then I send the data to the API to store the info to the database. From what I was reading this seemed to be the right direction.
store predefined status of switches in array of dictionary like:
let alreadyYouHaveThis = [["id": 1, "title": "name"...., ], [ "id": 1, "title": "name"....., ],["id": 1, "title": "name"...., ]
just add one more key in the same data like status...
let updatedData = [["status": true, "id": 1, "title": "name"...., ], ["status": true, "id": 1, "title": "name"....., ],["status": false, "id": 1, "title": "name"...., ]
now that status will be used in cellforindexpath method as other:
func cellforindexpath() ..... {
let status = data[indexPath.item]["status"]
swithc.tag = indexpath.item
setswitchstatus on above value
}
and action for switch :
#Objc func switchStatus(_ switch: UISwitch) {
let status = data[switch.tag]
status != status
data[switch.tag]["status"] = status
....
}
or can edit main data source and add these status in that data.
if you cant perform this let me know.

Advice on structuring data for scalability with large number of nested objects

Looking for advice on how best to structure data in MongoDB, particularly for scalability - worried about having an array of potentially thousands of objects within each user object.
I am building a language learning app with a built in flashcard system. I want users to 'unlock' new vocabulary for each level, which automatically gets added to their flashcards, so when you unlock level 4, all the vocabulary attached to level 4 gets added to your flashcards.
For the flashcards themselves, I want a changable 'due date', so that you get prompted to do certain cards at a certain date - if you're familiar with spaced repition, that's the plan. So when you get a card, you can say how well you know it and, for example, if you know it well you won't get it for another week, but if you get it wrong you'll get it again the next day.
I'm using MongoDB for the backend, but am a little unsure about how best to structure my data. Currently, I have two objects: one for the cards, and one for the users.
The cards object looks like this, so there's a nested object for each flashcard, with a unique ID, the level the word appears in, and then the word in both languages.
const CardsList = [
{
id: 1,
level: 1,
gd: "sgìth",
en: "tired",
},
{
id: 2,
level: 2,
gd: "ceist",
en: "question",
},
];
Then each user has an object like the below, with various user data, and a nested array of objects for the cards - with the id of every card they've unlocked, and the date at which that card is next due.
{
id: 1,
name: "gordon",
level: 2,
cards: [
{ id: 1, date: "07/12/2021" },
{ id: 2, date: "09/12/2021" },
],
},
{
id: 2,
name: "mike",
level: 1,
cards: [
{ id: 1, date: "08/12/2021" },
{ id: 2, date: "07/12/2021" },
],
},
This works fine, but I'm a bit concerned about the scalability of it.
The plan is to have about two or three thousand words in total, and so if I had, say, fifty users complete the app, then that would mean fifty user objects, each with as much as three thousand objects in that nested cards array.
Is that going to be a problem? Would it be a problem if I had a thousand (or more) users, instead of 50? Is there a more sensible way of structuring the data that I'm not spotting?

Select an item from Dojo Grid's store and display one of its attributes (array of objects) on grid

I have a Dojo EnhancedGrid which uses a data store filled with the following data structure:
[
{ id: 1, desc: "Obj Desc", options: [ { txt: "text", value: 0 }, { obj2 }, { objn } ] },
{ id: 2, ... },
{ id: 3, ... },
{ id: n, ... }
]
Currently I'm doing all this with an auxiliary store...but I believe this is far from a good approach to the problem, it's too ugly and doesn't work really well with edition (because I have to send changes from one store to another).
Instead of displaying all this objects at the same time, I wanted to select just one object (using its id) and display its options objects on grid. At the same time, the changes on grid should make effect on store, to be able to save them later.
Is it possible to query the grid's store, in order to display just one object? How?
And is it possible to fill the grid with objects list present on "options" attribute?

Post to facebook with multiple cities target

I'm trying to post something with target parameters, and i can do that but specifying only one city. Is there any way to pass two or more cities?
I'm trying this way:
parameters.targeting = new {
countries = "some country",
cities = new [] { new { key = "city value" }, new { key = "other city value" } },
locales = "locale code"
};
But no success! How can i do this?
This is very unclear, but I am guessing you are just not formatting your array of cities correctly. See more here: https://developers.facebook.com/docs/reference/ads-api/targeting-specs/
I believe you should be using "name" instead of "key" for the cities, but again I really don't have enough information to go off of.
Specify key, radius & distance_unit. For key, see Targeting Search,
Cities. radius is a distance around cities, from 10 to 50 miles or 17
to 80 kilometers. distance_unit is mile or kilometer. Limit: 250.
Example: 'cities': [{'key':'2430536', 'radius':12,
'distance_unit':'mile'}]
Source: https://developers.facebook.com/docs/marketing-api/buying-api/targeting#location