{"id":15699,"date":"2016-08-30T17:02:33","date_gmt":"2016-08-30T21:02:33","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=15699"},"modified":"2016-08-30T19:02:10","modified_gmt":"2016-08-30T23:02:10","slug":"sort-descriptors-in-swift","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2016\/08\/30\/sort-descriptors-in-swift\/","title":{"rendered":"Sort Descriptors in Swift"},"content":{"rendered":"<p><a href=\"http:\/\/chris.eidhof.nl\/post\/sort-descriptors-in-swift\/\">Chris Eidhof<\/a> (<a href=\"https:\/\/twitter.com\/chriseidhof\/status\/770338228964581380\">tweet<\/a>):<\/p>\n<blockquote cite=\"http:\/\/chris.eidhof.nl\/post\/sort-descriptors-in-swift\/\">\n<p>A sort descriptor uses two runtime features of Objective-C: the <code>key<\/code> is a key\npath, and <a href=\"https:\/\/developer.apple.com\/library\/mac\/documentation\/Cocoa\/Conceptual\/KeyValueCoding\/Articles\/KeyValueCoding.html\">key-value coding<\/a> is used to lookup the value of that key at runtime. [&#8230;]<\/p>\n<p>This is a pretty cool use of runtime programming, especially when you realize the array of sort descriptors can be built at runtime, say based on a user clicking a column heading.<\/p>\n<p>[&#8230;]<\/p>\n<p>Rather than copying and pasting, we can define a function with an interface that\nis much like <code>NSSortDescriptor<\/code>, but without the runtime programming. This\nfunction takes a key and a comparison method, and returns a sort descriptor (the\nfunction, not the class <code>NSSortDescriptor<\/code>). Here, <code>key<\/code> is not a string, but a\nfunction. To compare two keys, we use a function <code>isOrderedBefore<\/code>. Finally, the\nresult type is a function as well, even though that is slightly obscured by the\n<code>typealias<\/code>.<\/p><\/blockquote>\n<p>However, he does not show how to achieve functionality equivalent to <code>NSSortDescriptor<\/code>, e.g. pulling the <code>identifier<\/code> out of a table column and sorting by that. Of course, this is trivial by leaning on the Objective-C runtime, but can it be done efficiently and concisely with a pure Swift object?<\/p>\n<blockquote cite=\"http:\/\/chris.eidhof.nl\/post\/sort-descriptors-in-swift\/\">\n<p>One drawback of the function-based approach is that functions are opaque. We can\ntake an <code>NSSortDescriptor<\/code>, print it to the console, and we get some information\nabout the sort descriptor: the key path, the selector name and whether it&rsquo;s\nascending.<\/p>\n<\/blockquote>\n<p>This also prevents straightforwardly persisting the sort descriptor or translating it into a database query.<\/p>","protected":false},"excerpt":{"rendered":"<p>Chris Eidhof (tweet): A sort descriptor uses two runtime features of Objective-C: the key is a key path, and key-value coding is used to lookup the value of that key at runtime. [&#8230;] This is a pretty cool use of runtime programming, especially when you realize the array of sort descriptors can be built at [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"","apple_news_api_id":"","apple_news_api_modified_at":"","apple_news_api_revision":"","apple_news_api_share_url":"","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[69,31,1245,30,760,71,901],"class_list":["post-15699","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-cocoa","tag-ios","tag-key-value-coding-kvc","tag-mac","tag-objective-c-runtime","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/15699","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=15699"}],"version-history":[{"count":2,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/15699\/revisions"}],"predecessor-version":[{"id":15704,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/15699\/revisions\/15704"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=15699"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=15699"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=15699"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}