{"id":1480,"date":"2012-08-09T15:14:51","date_gmt":"2012-08-09T11:14:51","guid":{"rendered":"https:\/\/blog.alwawee.ru\/?p=1480"},"modified":"2012-08-09T15:38:55","modified_gmt":"2012-08-09T11:38:55","slug":"sort-array-of-objects-by-date-in-objective-c","status":"publish","type":"post","link":"https:\/\/blog.alwawee.ru\/?p=1480","title":{"rendered":"How to sort an array of custom objects by date in Objective C"},"content":{"rendered":"<p>This is an example of sorting an array of custom objects by date in Objective C in descending order. Sorting by date is not different from sorting by NSInteger. <\/p>\n<p>There are two ways to do this. First is to use NSSortDescriptor:<\/p>\n<pre code=\"ObjC\">\r\nNSSortDescriptor *sortDescriptor = \\\r\n [[NSSortDescriptor alloc] initWithKey:@\"dateTime\" \\\r\n ascending:NO];\r\nmessagesArray = [messagesArray \\\r\nsortedArrayUsingDescriptors:[NSArray \\\r\narrayWithObject:sortDescriptor]];\r\n<\/pre>\n<p>The second is to use NSComparisonResult: <\/p>\n<pre code = \"ObjC\">\r\n NSArray *sortedArray;\r\n sortedArray = [array sortedArrayUsingComparator: \\\r\n^NSComparisonResult(id a, id b) {\r\n  NSDate *first = [(MCMessage*)a dateTime];\r\n  NSDate *second = [(MCMessage*)b dateTime];\r\n  return [second compare:first];\r\n }];\r\n array = sortedArray;\r\n<\/pre>\n<p>To sort in ascending order change this line: <\/p>\n<pre code=\"ObjC\">\r\nreturn [second compare:first];\r\n\r\nreturn [first compare:second];\r\n<\/pre>\n<p><a href=\"http:\/\/stackoverflow.com\/questions\/805547\/how-to-sort-an-nsmutablearray-with-custom-objects-in-it\" title=\"http:\/\/stackoverflow.com\/questions\/805547\/how-to-sort-an-nsmutablearray-with-custom-objects-in-it\" target=\"_blank\">This post<\/a> helped me a lot.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is an example of sorting an array of custom objects by date in Objective C in descending order. Sorting by date is not different from sorting by NSInteger. There are two ways to do this. First is to use NSSortDescriptor: NSSortDescriptor *sortDescriptor = \\ [[NSSortDescriptor alloc] initWithKey:@&#8221;dateTime&#8221; \\ ascending:NO]; messagesArray = [messagesArray \\ sortedArrayUsingDescriptors:[NSArray [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-1480","post","type-post","status-publish","format-standard","hentry","category-ios"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to sort an array of custom objects by date in Objective C - Denis Kutlubaev<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/blog.alwawee.ru\/?p=1480\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to sort an array of custom objects by date in Objective C - Denis Kutlubaev\" \/>\n<meta property=\"og:description\" content=\"This is an example of sorting an array of custom objects by date in Objective C in descending order. Sorting by date is not different from sorting by NSInteger. There are two ways to do this. First is to use NSSortDescriptor: NSSortDescriptor *sortDescriptor =  [[NSSortDescriptor alloc] initWithKey:@&quot;dateTime&quot;  ascending:NO]; messagesArray = [messagesArray  sortedArrayUsingDescriptors:[NSArray [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/blog.alwawee.ru\/?p=1480\" \/>\n<meta property=\"og:site_name\" content=\"Denis Kutlubaev\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/alwaweemobile\" \/>\n<meta property=\"article:published_time\" content=\"2012-08-09T11:14:51+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2012-08-09T11:38:55+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/blog.alwawee.com\/?attachment_id=1982\" \/>\n<meta name=\"author\" content=\"Denis Kutlubaev\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Denis Kutlubaev\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=1480#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=1480\"},\"author\":{\"name\":\"Denis Kutlubaev\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/#\\\/schema\\\/person\\\/58f1f1f57155b48afab83ba53b3e26a7\"},\"headline\":\"How to sort an array of custom objects by date in Objective C\",\"datePublished\":\"2012-08-09T11:14:51+00:00\",\"dateModified\":\"2012-08-09T11:38:55+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=1480\"},\"wordCount\":74,\"commentCount\":0,\"articleSection\":[\"iOS\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/blog.alwawee.ru\\\/?p=1480#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=1480\",\"url\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=1480\",\"name\":\"How to sort an array of custom objects by date in Objective C - Denis Kutlubaev\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/#website\"},\"datePublished\":\"2012-08-09T11:14:51+00:00\",\"dateModified\":\"2012-08-09T11:38:55+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/#\\\/schema\\\/person\\\/58f1f1f57155b48afab83ba53b3e26a7\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=1480#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/blog.alwawee.ru\\\/?p=1480\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=1480#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/blog.alwawee.ru\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to sort an array of custom objects by date in Objective C\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/#website\",\"url\":\"https:\\\/\\\/blog.alwawee.ru\\\/\",\"name\":\"Denis Kutlubaev\",\"description\":\"iOS Development\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/blog.alwawee.ru\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/#\\\/schema\\\/person\\\/58f1f1f57155b48afab83ba53b3e26a7\",\"name\":\"Denis Kutlubaev\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg\",\"caption\":\"Denis Kutlubaev\"},\"description\":\"iOS Developer, creator of Tornado Browser and many other popular apps\",\"sameAs\":[\"http:\\\/\\\/dennis.alwawee.com\"],\"url\":\"https:\\\/\\\/blog.alwawee.ru\\\/?author=1\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to sort an array of custom objects by date in Objective C - Denis Kutlubaev","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/blog.alwawee.ru\/?p=1480","og_locale":"en_US","og_type":"article","og_title":"How to sort an array of custom objects by date in Objective C - Denis Kutlubaev","og_description":"This is an example of sorting an array of custom objects by date in Objective C in descending order. Sorting by date is not different from sorting by NSInteger. There are two ways to do this. First is to use NSSortDescriptor: NSSortDescriptor *sortDescriptor =  [[NSSortDescriptor alloc] initWithKey:@\"dateTime\"  ascending:NO]; messagesArray = [messagesArray  sortedArrayUsingDescriptors:[NSArray [&hellip;]","og_url":"https:\/\/blog.alwawee.ru\/?p=1480","og_site_name":"Denis Kutlubaev","article_publisher":"https:\/\/www.facebook.com\/alwaweemobile","article_published_time":"2012-08-09T11:14:51+00:00","article_modified_time":"2012-08-09T11:38:55+00:00","og_image":[{"url":"http:\/\/blog.alwawee.com\/?attachment_id=1982","type":"","width":"","height":""}],"author":"Denis Kutlubaev","twitter_misc":{"Written by":"Denis Kutlubaev","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/blog.alwawee.ru\/?p=1480#article","isPartOf":{"@id":"https:\/\/blog.alwawee.ru\/?p=1480"},"author":{"name":"Denis Kutlubaev","@id":"https:\/\/blog.alwawee.ru\/#\/schema\/person\/58f1f1f57155b48afab83ba53b3e26a7"},"headline":"How to sort an array of custom objects by date in Objective C","datePublished":"2012-08-09T11:14:51+00:00","dateModified":"2012-08-09T11:38:55+00:00","mainEntityOfPage":{"@id":"https:\/\/blog.alwawee.ru\/?p=1480"},"wordCount":74,"commentCount":0,"articleSection":["iOS"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/blog.alwawee.ru\/?p=1480#respond"]}]},{"@type":"WebPage","@id":"https:\/\/blog.alwawee.ru\/?p=1480","url":"https:\/\/blog.alwawee.ru\/?p=1480","name":"How to sort an array of custom objects by date in Objective C - Denis Kutlubaev","isPartOf":{"@id":"https:\/\/blog.alwawee.ru\/#website"},"datePublished":"2012-08-09T11:14:51+00:00","dateModified":"2012-08-09T11:38:55+00:00","author":{"@id":"https:\/\/blog.alwawee.ru\/#\/schema\/person\/58f1f1f57155b48afab83ba53b3e26a7"},"breadcrumb":{"@id":"https:\/\/blog.alwawee.ru\/?p=1480#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/blog.alwawee.ru\/?p=1480"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/blog.alwawee.ru\/?p=1480#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/blog.alwawee.ru\/"},{"@type":"ListItem","position":2,"name":"How to sort an array of custom objects by date in Objective C"}]},{"@type":"WebSite","@id":"https:\/\/blog.alwawee.ru\/#website","url":"https:\/\/blog.alwawee.ru\/","name":"Denis Kutlubaev","description":"iOS Development","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/blog.alwawee.ru\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/blog.alwawee.ru\/#\/schema\/person\/58f1f1f57155b48afab83ba53b3e26a7","name":"Denis Kutlubaev","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg","url":"https:\/\/secure.gravatar.com\/avatar\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg","caption":"Denis Kutlubaev"},"description":"iOS Developer, creator of Tornado Browser and many other popular apps","sameAs":["http:\/\/dennis.alwawee.com"],"url":"https:\/\/blog.alwawee.ru\/?author=1"}]}},"_links":{"self":[{"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/posts\/1480","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1480"}],"version-history":[{"count":0,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/posts\/1480\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1480"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1480"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1480"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}