sorting custom objects from array into dictionary by property - ios

Is there an easy way to sort an array of custom objects (in this case Lists) into a dictionary based on a particular property of each list.
[List1, List2, List3, List4, List5];
For example, each List object has an NSString type property, which can be either "MyList","Sent","Received"
How would I create a dictionary based on these properties so that I have a dictionary like so:
"MyList" -> array of lists with MyList as their type property [List1, List5];
"Sent" -> array of lists with Sent as their type property "Received" [List3;
"Received" -> array of lists with Received as their type property [List2, List4];
I'd really rather not loopthrough my entire array of List objects if possible

You need to iterate the array and build your dictionary. If the key isn't already there, create the array and add it to the dictionary, then add the new item to it.
You could alternately use predicates to filter the array into sub-arrays and build the dictionary like that but it's a similar amount of code (for a few options) and doesn't support automatic future expansion when you have another value for the key you're organising on.

Related

OData filter objects that contain particular items in subarray

I have the following situation:
I have an object
This object has an item that is an object again
The second object contains array of objects with ids
Can I filter only the parent objects that have subobjects that contain something in particular in the array on the bottom of the tree?
Try this:
$filter=nameOfObjectArrayProperty/any(o: contains(o/id, 'some-alue'))
or
$filter=nameOfObjectArrayProperty/any(o: o/id eq 'some-value')
It's also important to note that "/any" can be replaced with "/all" if you want all the subobjects to match your filter criteria.

Higher order function sort() used it with other higher order function its working with let content also

So, any reason behind happen this things, because when i perform sort() function with let type at that time xcode give me a compile time error
i tried it in my playgound with only sort function and sort() function with other higher order function.
like so
let name = ["mehul","HeLLi","JeniFER","Ankul"]
name.sort()
error :
Cannot use mutating member on immutable value: 'name' is a 'let' constant
but with other higher order functions
let nameStartingWithBArray = name.filter {$0.first != "B"}.map {$0.uppercased()}.sorted()
now there is no error
Please consider the subtle but crucial difference between sort and sorted
sort has no return value. It sorts in place that means the calling object is mutated.
sorted ( as well as filter and map) returns a new object with the result of the operation.
sort() basically works with the list itself. It modifies the original list in place. The return value is None.
name.sort() sorts the list and save the sorted list.
sorted() works on any iterable that may include list, dict and so on. It returns another list and doesn't modify the original list.
sorted() returns a sorted copy of the list (a new sorted list), without changing the original list.
Keynote : sort() is faster than sorted()
The sort function sorts the collection in place - i.e. it modifies the array, name, on which you call sort.
filter on the other hand returns an array and does not modify the original array.
Compare sort with sorted which again returns a new array and does not modify the original array.
So, you can write
let sortedNames = name.sorted()
sortedNames will be in order, while name will be in the original, unsorted, order.
If you write
var name = ["mehul","HeLLi","JeniFER","Ankul"]
name.sort()
then name will be in order, and because it is declared as var rather than a constant with let the change is permitted.
The sort func you are using is the mutating func, that means sort function internally sort the array in the same variable.
Mutating Func : To modify the properties of your structure or enumeration within a particular method, you can opt in to mutating behavior for that method. The method can then mutate (that is, change) its properties from within the method, and any changes that it makes are written back to the original structure when the method ends.
So. let doesn't allow sort for doing mutating, so you need to make it var.
More ever, if you use sorted(by:) , it returns new array of sorted values. So here you don't get any error.

can neo4j property value be a dictionary

I want to assign a dictionary as node property value. When I try to assign it a dictionary it throws an error if cyphertype saying only arrays or primitive data types can be assigned. So the question is that is there any work around to this problem
There is no way to assign a dictionary as a single node property value.
But you can assign each key in the dictionary as a single property and save the dictionary that way.
WITH {params}
MATCH (n:Node)
SET n += params

NSMutableDictionary add object by sequence issue

I added sequence in NSMutableDictionary by keys
Here is the key of Dictionary
New,
To Do,
Basic,
Advanced,
In Progress,
Done
But when I print NSMutableDictionary all keys then output was different
Advanced,
In Progress,
To Do,
New,
Done,
Basic
Any solution for this ?
Dictionaries are unordered collections. The sequence of items is no guaranteed, and will not be preserved. Don't make any assumptions about the order objects are fetched from a dictionary. It is not specified and subject to change.
If you need an ordered collection, try using an array of single-element dictionaries, or an array of custom objects with a key and value property.
You can try the following or come up with own data structure if you are worried about the position of elements inside a Dictionary
You can use Array of key-value pairs like this -> [[String : AnyObject]].
Which looks like : [["New" : 1],["In Progress" : 3],...]
You can also use tuples of key and value in an Array like this -> [(key,value)]
Which looks like : [("New" , 1),("In Progress" , 3),...]
Try to get keys in this array
NSArray *orderedArr = [[dict allKeys] sortedArrayUsingSelector:#selector(localizedStandardCompare:)];

Create and iterate through an array in Velocity Template Language

How to create an array in VTL and add contents to the array? Also how to retrieve the contents of the array by index?
According to Apache Velocity User Guide, right hand side of assignments can be of type
Variable reference
List item
String literal
Property reference
Method reference
Number literal
ArrayList
Map
You can create an empty list, which would satisfy all your needs for an array, in an Apache Velocity template with an expression like:
#set($foo = [])
or initialize values:
#set($foo = [42, "a string", 21, $myVar])
then, add elements using the Java add method:
$foo.add(53);
$foo.add("another string");
but beware, as the Java .add() method for the list type returns a boolean value, when you add an element to the list, Velocity will print, for instance, "true" or "false" based on the result of the "add" function.
A simple work around is assigning the result of the add function to a variable:
#set($bar = $foo.add(42))
You can access the elements of the list using index numbers:
<span>$foo[1]</span>
Expression above would show a span with the text "a string". However the safest way to access elements of a list is using foreach loops.
Creating an array is easy:
#set($array = [])
Putting an element into an array is also easy:
$array.add(23)
Getting an element from an array depends from your Velocity version.
In Velocity 1.6 you must use
$array.get($index)
Since Velocity 1.7 you can use the classic form:
$array[$index]
I haven't created an array in VTL but passed arrays to VTL context and used them. In VTL, you can not retrieve array contents by index, you only use foreach, as example this code is copied from my Dynamic SQL generation VTL Script:
#foreach( $col in $Columns ) SUM($col.DBColumn) AS ''$col.Name''#if($velocityCount!=$Columns.Count), #end #end
For this reason, we also can not have 2D arrays. When I needed an array to store 2 objects in a row, I used the workaround of defining a new class, and putting objects of that class in the single dimensional array.

Resources