Swift Sorting Array<Array<AnyObject>> - swift

I have the below response. how to sort according to the first element. i.e. [["Albania", "AE"], ["United States", "US"]]
[
[
"United States",
"US"
],
[
"Albania",
"AE"
]
]

var array = [ [ "United States", "US" ], [ "Albania", "AE" ] ]
array.sortInPlace({$0[0] < $1[0]})

One easy way to achieve this using Closures.
Here is a Shorthand way of sorting.
let array = [["United States","US"],["Albania", "AE"],["India", "IN"],["United Kingdom", "UK"]]
print(array)
let arrAscending = array.sort({($0[0]) < ($1[0])})
let arrDescending = array.sort({($0[0]) > ($1[0])})
print(arrAscending)
print(arrDescending)

Try
var a = [ [ "United States", "US" ], [ "Albania", "AE" ] ]
a.sort { (first, second) -> Bool in
return first.first < second.first
}

Related

Dictionary subtract issue for its similar array element

I am trying to subtract two JSON/Dictionary. But the dictionary containing an array of objects is not working properly. Demo examples are given below. My intention is to prepare a new JSON file that property does not exist in the default JSON file. That is for the country-specific configuration I want to put into the new file.
[NOTE: If the value is different in any key of two dictionaries it should not be subtracted and we will consider the first dictionary value for the key. If the item is the same in any array then those similar array items should be removed. That is my main intention. I think it is clear now.]
let dict1 = [
"FALLBACK": "en-GB",
"SUPPORTED": [
"en-GB",
"fr",
"de",
"es",
"it",
"hu",
"nl",
"pl",
"sv",
"tr",
"ru",
"da",
"cs",
"fi",
"nb",
"pt",
"sr"
]
]
let dict2 = [
"FALLBACK": "fr",
"SUPPORTED": [
"en-GB",
"fr",
"de",
"es",
"it",
"hu",
"nl",
"pl",
"sv",
"tr",
"ru",
"da",
"cs",
"fi",
"nb",
"pt",
"sr",
"ar",
"bg",
"el",
"fa",
"hr",
"ka",
"lt",
"lv",
"ro",
"sk",
"sl",
"uk"
]
]
let diff = dict2.minus(dict: dict1)
Expected output (something like this):
[
"FALLBACK": "en-GB",
"SUPPORTED": [
"ar",
"bg",
"el",
"fa",
"hr",
"ka",
"lt",
"lv",
"ro",
"sk",
"sl",
"uk"
]
]
I followed this subtraction function. All are fine but the problem is during array subtraction.
extension Dictionary where Key: Comparable, Value: Equatable {
func minus(dict: [Key:Value]) -> [Key:Value] {
let entriesInSelfAndNotInDict = filter { dict[$0.0] != self[$0.0] }
return entriesInSelfAndNotInDict.reduce([Key:Value]()) { (res, entry) -> [Key:Value] in
var res = res
res[entry.0] = entry.1
return res
}
}
}

Read JSON in ADF

In Azure Data Factory, I need to be able to process a JSON response. I don't want to hardcode the array position in case they change, so something like this is out of the question:
#activity('Place Details').output.result.components[2].name
How can I get the name 123 where types = number given a JSON array like below:
"result": {
"components": [
{
"name": "ABC",
"types": [
"alphabet"
]
},
{
"name": "123",
"types": [
"number"
]
}
]
}
One example using the OPENJSON method:
DECLARE #json NVARCHAR(MAX) = '{
"result": {
"components": [
{
"name": "ABC",
"types": [
"alphabet"
]
},
{
"name": "123",
"types": [
"number"
]
}
]
}
}'
;WITH cte AS (
SELECT
JSON_VALUE( o.[value], '$.name' ) [name],
JSON_VALUE( o.[value], '$.types[0]' ) [types]
FROM OPENJSON( #json, '$.result.components' ) o
)
SELECT [name]
FROM cte
WHERE types = 'number'
I will have a look at other methods.

How to parse date pattern using grok

How to parse the below log line using grok
Also how to match the pattern of the date.
I tried %{TIMESTAMP_ISO8601:logtime} but no match
Log Line:
13-Nov-2019 00:00:20.230 DEBUG [[ACTIVE] ExecuteThread: '272' for queue: 'weblogic.kernel.Default (self-tuning)'] [196.157.7.12] 965929132 [wire] >> "[\n]"
The question is a bit unclear as to exactly what fields you want them mapped to.
So, here's what matches for me:
%{MONTHDAY:day}[-]%{MONTH:month}[-]%{YEAR:year} %{TIME:time} %{WORD:logtype} \[\[%{WORD:status}\] ExecuteThread: '%{NUMBER:threadNumber}' for queue: '%{GREEDYDATA:queueData}'\] \[%{IP:ip}\] %{NUMBER:numbers} \[%{WORD:text}\] >> "\[\\n\]"
The first 4 fields, answer your date/time pattern query and the rest is what I have used to fit the rest of the fields. Since, no exact mappings were provided , I have mapped them as per my understanding using
This is the output:
{
"day": [
[
"13"
]
],
"month": [
[
"Nov"
]
],
"year": [
[
"2019"
]
],
"time": [
[
"00:00:20.230"
]
],
"logtype": [
[
"DEBUG"
]
],
"status": [
[
"ACTIVE"
]
],
"threadNumber": [
[
"272"
]
],
"queueData": [
[
"weblogic.kernel.Default (self-tuning)"
]
],
"ip": [
[
"196.157.7.12"
]
],
"numbers": [
[
"965929132"
]
],
"text": [
[
"wire"
]
]
}
You can break 'time' further if you want. For any other combinations of patterns, refer Grok Patterns.

how to declare a dictionary inside two arrays

How can I declare a multi array outside its function so i can use it outside its function? I know how to do a regular array and a regular dictionary, but not both.
IhelpersCoordinates = [
[
"Latitude":IhelpersLatitude,
"Longitude": IhelpersLongitude,
"userId": IhelpersUid
]
]
The code above is in the viewDidLoad. I'm trying to use it in the user did update function. I know with a regular array i have to set the array outside of the function (example- var IhelpersCordinates = []). I'm trying to figure out how i would do the same with the array above.
As I understand your problem correctly you can declare a dictionary inside array use [[String:Double]].
class YourClass {
var IhelpersCoordinates : [[String:Double]] = [
[
"Latitude": 53.02,
"Longitude": 19.04,
"userId": 123
],
[
"Latitude": 51.02,
"Longitude": 20.04,
"userId": 124
],
]
func exampleFunc(){
print(IhelpersCoordinates[0]["Latitude"]) // this will print 53.02
print(IhelpersCoordinates.count) // this will print 2, because it's 2 elements array of dictionaries.
}
}
Edited base on comment
If you want to declare an dictionary inside an array inside an array try this code :
class YourClass {
var IhelpersCoordinates : Array<Array<[String:Double]>> = [
[
[
"Latitude": 53.02,
"Longitude": 19.04,
"userId": 123
],
],
[
[
"Latitude": 53.02,
"Longitude": 19.04,
"userId": 123
],
],
]
func exampleFunc(){
print(IhelpersCoordinates[0][0]["Latitude"])
print(IhelpersCoordinates.count)
}
}
EDIT 2
class YourClass {
public var IhelpersCoordinates = Array<Array<[String:Double]>>()
func calculate() {
var element = [
[
"Latitude": 53.02,
"Longitude": 19.04,
"userId": 123
]
]
var element1 = [
[
"Latitude": 54.02,
"Longitude": 19.04,
"userId": 122
]
]
self.IhelpersCoordinates.append(element)
self.IhelpersCoordinates.append(element1)
}
func printValues() {
print(self.IhelpersCoordinates.count)
}
}
Hope it help you

iOS Find Location (Latitude,longitude) from Zipcode

What could be the best way to get location for a given zipcode. Would it work for countries outside US/Canada .Thanks
Use the Google geolocating API (try it out on google.com/maps):
Input for a Swiss ZIP code for example:
CH-9014
or a french one:
FR-34000
or german:
de-12101
or US:
us-90210
or canada:
ca-J5Z 1A1
or china:
cn-100000
for example:
yields
{
"status": "OK",
"results": [ {
"types": [ "postal_code" ],
"formatted_address": "9014 St Gallen, Switzerland",
"address_components": [ {
"long_name": "9014",
"short_name": "9014",
"types": [ "postal_code" ]
}, {
"long_name": "St Gallen",
"short_name": "St Gallen",
"types": [ "locality", "political" ]
}, {
"long_name": "Sankt Gallen",
"short_name": "SG",
"types": [ "administrative_area_level_1", "political" ]
}, {
"long_name": "Switzerland",
"short_name": "CH",
"types": [ "country", "political" ]
} ],
"geometry": {
"location": {
"lat": 47.4082855,
"lng": 9.3323890
},
"location_type": "APPROXIMATE",
"viewport": {
"southwest": {
"lat": 47.3991076,
"lng": 9.3180504
},
"northeast": {
"lat": 47.4199564,
"lng": 9.3543340
}
},
"bounds": {
"southwest": {
"lat": 47.3991076,
"lng": 9.3180504
},
"northeast": {
"lat": 47.4199564,
"lng": 9.3543340
}
}
}
} ]
}
So the swiss ZIP code 9014 corresponds appx. to this location:
"lat": 47.4082855,
"lng": 9.3323890
See my answer on the geolocating API here:
How to get GLatLng object from address string in advance in google maps?
GeoNames offers a number of various zipcode geocoding services, including search for the location of a given zipcode. They support a number of various countries.
You would probably be most interested in the Placename Lookup for postalcode service
Google's API comes with some fairly nasty restrictions. Better to use open sources like GeoNames per #Claus Broch's suggestion.
I'm adding this answer to also note that MapQuest has recently put up an API to OpenStreetMap data. It doesn't do (as far as I can tell) zip code searching, but location names, directions, altitude etc are all freely queryable. I recently discovered it an I plan on replacing my calls to google maps API with it as soon as I can.
http://developer.mapquest.com/web/products/open