I want to sort the data from the json array according to the date, and the nominal amount of data that matches the date. I use swiftyjson and alamofire.
here is my code:
import UIKit
import Alamofire
import SwiftyJSON
class HomeViewController: UIViewController{
override func viewDidLoad() {
super.viewDidLoad()
fetchData()
let createOrder = UserDefaults.standard.stringArray(forKey: "createOrder")
let nominal = UserDefaults.standard.stringArray(forKey: "nominal")
createOrder = createOrder?.sorted{ $0 < $1 }
print(createOrder?.description)
}
}
func fetchData(){
let url = ""
Alamofire.request(url, method: .get, encoding: URLEncoding.default).responseJSON{
(response) in
switch response.result {
case .success(let value):
let json = JSON(value)
print(json)
let jsonData = json["data"]["transaction"]
let data = jsonData.arrayValue.map{ $0["nominal"].string}
let createOrder = jsonData.arrayValue.map { $0["order_created"].string}
UserDefaults.standard.set(data, forKey: "nominal")
UserDefaults.standard.set(createOrder, forKey: "createOrder")
case .failure(let error):
print(error)
}
}
}
here is response JSON
"data" : {
"transaction" : [
{
"order_created" : "2019-03-30 14:39:05",
"nominal" : "300000",
},
{
"order_created" : "2019-03-30 11:26:08",
"nominal" : "250000",
},
{
"order_created" : "2019-03-29 10:49:44",
"nominal" : "200000",
}
]
what has been achieved:
Optional("[\"2019-03-30 10:49:44\", \"2019-03-30 11:26:08\", \"2019-03-30 14:39:05\"]")
i get data on 2019-03-30 are 2 nominal 300000 and 250000
in 2019-03-29 i get 1 nominal 200000
i want total nominal on 2019-03-30 are 300000 + 250000 = 550000
so the result should i get: on 2019-03-30 total is 500000 and 2019-03-29 total is 200000
please help me