{"id":1176,"date":"2015-02-26T14:52:34","date_gmt":"2015-02-26T05:52:34","guid":{"rendered":"https:\/\/ecoop.net\/memo\/?p=1176"},"modified":"2017-02-07T22:48:30","modified_gmt":"2017-02-07T13:48:30","slug":"sort_records_in_swift","status":"publish","type":"post","link":"https:\/\/www.ecoop.net\/memo\/archives\/sort_records_in_swift.html","title":{"rendered":"[iOS][Swift] Swift\u3063\u307d\u304f\u30ec\u30b3\u30fc\u30c9\u3092\u30bd\u30fc\u30c8\u3092\u3059\u308b"},"content":{"rendered":"<p>NSSortDescriptor\u306fObjective-C\u3067\u3082\u4f7f\u3048\u308b\u30ec\u30b3\u30fc\u30c9\u306e\u914d\u5217\u3092\u30ec\u30b3\u30fc\u30c9\u30ad\u30fc\u3067\u30bd\u30fc\u30c8\u3059\u308b\u306e\u306b\u4fbf\u5229\u306a\u65b9\u6cd5\u3067\u3059\u304c\u3001Array\u306esort()\u30e1\u30bd\u30c3\u30c9\u3084sorted()\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u3046\u3068\u3088\u308aSwift\u3063\u307d\u304f\u30af\u30ed\u30fc\u30b8\u30e3\u3067\u30bd\u30fc\u30c8\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<h3>\u5909\u66f4\u524d\uff1a<\/h3>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nlet records = NSMutableArray()\nlet sortDescriptor = NSSortDescriptor(key: &quot;fileSize&quot;, ascending: true)\nrecords.sortedArrayUsingDescriptors(&#x5B;sortDescriptor])\n\n\/\/ \u30ec\u30b3\u30fc\u30c9\u6295\u5165\nlet record1 = NSMutableDictionary()\nrecord1&amp;#091;&quot;filePath&quot;&amp;#093; = &quot;\/path\/to\/file1&quot;\nrecord1&amp;#091;&quot;fileSize&quot;&amp;#093; = 1000\nrecords.addObject(record1)\n\nlet record2 = NSMutableDictionary()\nrecord2&amp;#091;&quot;filePath&quot;&amp;#093; = &quot;\/path\/to\/file2&quot;\nrecord2&amp;#091;&quot;fileSize&quot;&amp;#093; = 100\nrecords.addObject(record2)\n\nlet record3 = NSMutableDictionary()\nrecord3&amp;#091;&quot;filePath&quot;&amp;#093; = &quot;\/path\/to\/file3&quot;\nrecord3&amp;#091;&quot;fileSize&quot;&amp;#093; = 500\nrecords.addObject(record3)\n\nprintln(records)\n\n<\/pre>\n<p><!--more--><\/p>\n<h3>\u5909\u66f4\u5f8c\uff1a<\/h3>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\/\/ &amp;#091;&amp;#091;String:Any&amp;#093;&amp;#093; \u3067\u3082\u3044\u3044\u304c\u3001\u30ec\u30b3\u30fc\u30c9\u578b\u3092\u660e\u793a\u3059\u308b\u305f\u3081\u30bf\u30d7\u30eb\u306b\u3059\u308b\u3002\n\/\/ \u30bf\u30d7\u30eb\u3067\u306a\u304f\u30af\u30e9\u30b9\u306b\u7f6e\u304d\u63db\u3048\u308b\u306e\u3082\u3088\u3057\u3002\nvar records:&amp;#091;(fileSize:Int,filePath:String)&amp;#093; = &amp;#091;&amp;#093;\n\n\/\/ \u30ec\u30b3\u30fc\u30c9\u6295\u5165\nlet record1:(fileSize:Int,filePath:String) = (1000, &quot;\/path\/to\/file&quot;)\nrecords.append(record1)\n\nlet record2:(fileSize:Int,filePath:String) = (100, &quot;\/path\/to\/file2&quot;)\nrecords.append(record2)\n\nlet record3:(fileSize:Int,filePath:String) = (500, &quot;\/path\/to\/file3&quot;)\nrecords.append(record3)\n\n\/\/ \u6607\u9806\u30bd\u30fc\u30c8\u5b9f\u884c\u3002sort\u306a\u3089\u81ea\u5206\u81ea\u8eab\u3092\u30bd\u30fc\u30c8\u3001sorted\u306a\u3089\u81ea\u5206\u306f\u5909\u3048\u305a\u306b\u30bd\u30fc\u30c8\u3057\u305f\u7d50\u679c\u3092\u8fd4\u3059\nrecords.sort { (a, b) -&gt; Bool in a.fileSize &amp;lt; b.fileSize }\n\nprintln(recors)\n<\/pre>\n<p>\u809d\u306fsort\u306b\u6e21\u3059\u30af\u30ed\u30fc\u30b8\u30e3\u3067\u3059\u3002<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nrecords.sort { (a, b) -&gt; Bool in a.fileSize &amp;lt; b.fileSize }\n<\/pre>\n<p>\u306f\u7701\u7565\u3057\u305f\u66f8\u304d\u65b9\u3067\u3001\u7701\u7565\u305b\u305a\u306b\u66f8\u304f\u3068\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nrecords.sort({ \n (a:(fileSize:Int,filePath:String), b(fileSize:Int,filePath:String)) -&gt; Bool in \n  return a.fileSize &amp;lt; b.fileSize \n})\n<\/pre>\n<p>NSSortDescriptor\u3068\u9055\u3044\u3001\u4efb\u610f\u306e\u30af\u30ed\u30fc\u30b8\u30e3\u3092\u5b9a\u7fa9\u3067\u304d\u308b\u306e\u3067\u3001<br \/>\n\u3088\u308a\u67d4\u8edf\u306a\u30bd\u30fc\u30c8\u304c\u3067\u304d\u308b\u306e\u304c\u3044\u3044\u3067\u3059\u306d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>NSSortDescriptor\u306fObjective-C\u3067\u3082\u4f7f\u3048\u308b\u30ec\u30b3\u30fc\u30c9\u306e\u914d\u5217\u3092\u30ec\u30b3\u30fc\u30c9\u30ad\u30fc\u3067\u30bd\u30fc\u30c8\u3059\u308b\u306e\u306b\u4fbf\u5229\u306a\u65b9\u6cd5\u3067\u3059\u304c\u3001Array\u306esort()\u30e1\u30bd\u30c3\u30c9\u3084sorted()\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u3046\u3068\u3088\u308aSwift\u3063\u307d\u304f\u30af\u30ed\u30fc &hellip; <a href=\"https:\/\/www.ecoop.net\/memo\/archives\/sort_records_in_swift.html\" class=\"more-link\">\u7d9a\u304d\u3092\u8aad\u3080 <span class=\"screen-reader-text\">[iOS][Swift] Swift\u3063\u307d\u304f\u30ec\u30b3\u30fc\u30c9\u3092\u30bd\u30fc\u30c8\u3092\u3059\u308b<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[117],"tags":[116,94,143,142],"class_list":["post-1176","post","type-post","status-publish","format-standard","hentry","category-ios-dev","tag-ios","tag-mac","tag-objective-c","tag-swift"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.ecoop.net\/memo\/wp-json\/wp\/v2\/posts\/1176","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ecoop.net\/memo\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ecoop.net\/memo\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ecoop.net\/memo\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ecoop.net\/memo\/wp-json\/wp\/v2\/comments?post=1176"}],"version-history":[{"count":1,"href":"https:\/\/www.ecoop.net\/memo\/wp-json\/wp\/v2\/posts\/1176\/revisions"}],"predecessor-version":[{"id":1195,"href":"https:\/\/www.ecoop.net\/memo\/wp-json\/wp\/v2\/posts\/1176\/revisions\/1195"}],"wp:attachment":[{"href":"https:\/\/www.ecoop.net\/memo\/wp-json\/wp\/v2\/media?parent=1176"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ecoop.net\/memo\/wp-json\/wp\/v2\/categories?post=1176"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ecoop.net\/memo\/wp-json\/wp\/v2\/tags?post=1176"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}