如何按字符串日期订购我的JSON?

时间:2017-01-05 02:04:55

标签: ios json swift

我有一个从JSON中提取数据的应用程序。 JSON的顺序非常重要,因为应用程序按照JSON的顺序显示其数据。

我创建了一个单独的应用来创建JSON对象,将其添加到JSON的其余部分并上传它。问题是它将它添加到JSON文件的顶部。我想在按日期值上传之前重新排序JSON。

我的JSON采用以下格式:

{
"articles":
    [                       
        {
            "title":"Title 3",
            "url":"URL 3",
            "date":"04/01/17"
        },  

        {
            "title":"Title 2",
            "url":"URL 2",
            "date":"03/01/17"
        },  

        {
            "title":"Title 1",
            "url":"URL 1",
            "date":"02/01/17"
        },  
    ]
}

日期是dd / MM / yy格式的字符串。如果我按顺序添加文章,那么没有问题,因为最新的文章将始终添加到顶部,但是我有时会添加前几天的文章,我想要插入以前的位置。< / p>

有没有办法在通过日期值上传之前订购JSON?

1 个答案:

答案 0 :(得分:1)

由于它已经是数组,只需要减去它,转换为 ServerAliveCountMax Sets the number of server alive messages (see below) which may be sent without ssh(1) receiving any messages back from the server. If this threshold is reached while server alive messages are being sent, ssh will disconnect from the server, terminating the session. It is important to note that the use of server alive messages is very different from TCPKeepAlive (below). The server alive messages are sent through the encrypted channel and there- fore will not be spoofable. The TCP keepalive option enabled by TCPKeepAlive is spoofable. The server alive mechanism is valu- able when the client or server depend on knowing when a connec- tion has become inactive. The default value is 3. This option applies to protocol version 2 only. ServerAliveInterval Sets a timeout interval in seconds after which if no data has been received from the server, ssh(1) will send a message through the encrypted channel to request a response from the server. The default is 0, indicating that these messages will not be sent to the server. This option applies to protocol version 2 only. TCPKeepAlive Specifies whether the system should send TCP keepalive messages to the other side. If they are sent, death of the connection or crash of one of the machines will be properly noticed. However, this means that connections will die if the route is down tempo- rarily, and some people find it annoying. The default is ``yes'' (to send TCP keepalive messages), and the client will notice if the network goes down or the remote host dies. This is important in scripts, and many users want it too. ,并比较它们,非常简单:

Date

<强> SwiftyJSON

let articleArray: [[String:String]] = dict["articles"]!
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd/MM/yy"

dict["articles"] = articleArray.sorted { (first, second) -> Bool in
    return dateFormatter.date(from: first["date"]!)! < dateFormatter.date(from: second["date"]!)!
}