{"id":925,"date":"2023-11-22T06:43:36","date_gmt":"2023-11-22T06:43:36","guid":{"rendered":"https:\/\/www.devopsconsulting.in\/blog\/?p=925"},"modified":"2024-10-01T10:28:39","modified_gmt":"2024-10-01T10:28:39","slug":"example-of-laravel-collection-sort-by-date","status":"publish","type":"post","link":"https:\/\/www.devopsconsulting.in\/blog\/example-of-laravel-collection-sort-by-date\/","title":{"rendered":"Example of Laravel Collection Sort By Date"},"content":{"rendered":"\n<p><strong>Here&#8217;s a detailed tutorial on how to sort a Laravel Collection by date with examples.<\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Example of Laravel Collection Sort By Date<\/strong><\/h3>\n\n\n\n<p>In Laravel, you can easily sort collections by date using the <code>sortBy<\/code> method. This method allows you to sort your collection based on the value of a particular field, such as a date field. Below, I&#8217;ll show you how to work with a collection and sort it by a date field.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 1: Sample Data<\/strong><\/h3>\n\n\n\n<p>Let&#8217;s assume you have a collection of blog posts, each with a <code>title<\/code> and a <code>created_at<\/code> date:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>use Illuminate\\Support\\Collection;\n\n\/\/ Sample data\n$posts = collect(&#91;\n    &#91;'title' =&gt; 'Post One', 'created_at' =&gt; '2023-08-01 14:00:00'],\n    &#91;'title' =&gt; 'Post Two', 'created_at' =&gt; '2023-06-15 10:30:00'],\n    &#91;'title' =&gt; 'Post Three', 'created_at' =&gt; '2023-09-25 16:45:00'],\n    &#91;'title' =&gt; 'Post Four', 'created_at' =&gt; '2023-07-05 09:00:00'],\n]);<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 2: Sorting by Date<\/strong><\/h3>\n\n\n\n<p>You can sort this collection by the <code>created_at<\/code> date using the <code>sortBy<\/code> method. Here&#8217;s how:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$sortedPosts = $posts-&gt;sortBy('created_at');<\/code><\/pre>\n\n\n\n<p>The <code>sortBy<\/code> method will sort the collection in ascending order by default.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 3: Displaying the Sorted Collection<\/strong><\/h3>\n\n\n\n<p>Let&#8217;s display the sorted posts:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$sortedPosts-&gt;each(function ($post) {\n    echo \"Title: {$post&#91;'title']}, Created At: {$post&#91;'created_at']}\\n\";\n});<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Output:<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>Title: Post Two, Created At: 2023-06-15 10:30:00\nTitle: Post Four, Created At: 2023-07-05 09:00:00\nTitle: Post One, Created At: 2023-08-01 14:00:00\nTitle: Post Three, Created At: 2023-09-25 16:45:00<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Sorting in Descending Order<\/strong><\/h3>\n\n\n\n<p>If you want to sort the collection by date in descending order, use the <code>sortByDesc<\/code> method:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$sortedPostsDesc = $posts-&gt;sortByDesc('created_at');\n\n$sortedPostsDesc-&gt;each(function ($post) {\n    echo \"Title: {$post&#91;'title']}, Created At: {$post&#91;'created_at']}\\n\";\n});<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Output:<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>Title: Post Three, Created At: 2023-09-25 16:45:00\nTitle: Post One, Created At: 2023-08-01 14:00:00\nTitle: Post Four, Created At: 2023-07-05 09:00:00\nTitle: Post Two, Created At: 2023-06-15 10:30:00<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Explanation<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>sortBy('created_at')<\/code>: Sorts the collection by the <code>created_at<\/code> field in ascending order.<\/li>\n\n\n\n<li><code>sortByDesc('created_at')<\/code>: Sorts the collection by the <code>created_at<\/code> field in descending order.<\/li>\n\n\n\n<li><code>each<\/code>: Iterates over the collection and prints each post&#8217;s title and created_at date.<\/li>\n<\/ul>\n\n\n\n<p>This example demonstrates how easy it is to sort Laravel collections by date using the <code>sortBy<\/code> and <code>sortByDesc<\/code> methods.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s a detailed tutorial on how to sort a Laravel Collection by date with examples. Example of Laravel Collection Sort By Date In Laravel, you can easily&#8230; <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[87],"tags":[466,461,470,464,467,473,176,459,472,465,177,471,469,468,178,460,463,462],"class_list":["post-925","post","type-post","status-publish","format-standard","hentry","category-laravel","tag-ascending-order-sort","tag-collection-sorting","tag-data-sorting-in-laravel","tag-date-sorting-in-laravel","tag-descending-order-sort","tag-eloquent-collection-sorting","tag-example-of-laravel-collection-sort-by-date","tag-laravel-collection","tag-laravel-collection-example","tag-laravel-collection-methods","tag-laravel-collection-sort-by-date","tag-laravel-sortby-function","tag-laravel-sorting-example","tag-php-laravel-collections","tag-sort-by-date-in-laravel","tag-sortby","tag-sortbydesc","tag-sorting-by-date"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Example of Laravel Collection Sort By Date - DevOps Consulting<\/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:\/\/www.devopsconsulting.in\/blog\/example-of-laravel-collection-sort-by-date\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Example of Laravel Collection Sort By Date - DevOps Consulting\" \/>\n<meta property=\"og:description\" content=\"Here&#8217;s a detailed tutorial on how to sort a Laravel Collection by date with examples. Example of Laravel Collection Sort By Date In Laravel, you can easily...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.devopsconsulting.in\/blog\/example-of-laravel-collection-sort-by-date\/\" \/>\n<meta property=\"og:site_name\" content=\"DevOps Consulting\" \/>\n<meta property=\"article:published_time\" content=\"2023-11-22T06:43:36+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-10-01T10:28:39+00:00\" \/>\n<meta name=\"author\" content=\"Abhishek Singh\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Abhishek Singh\" \/>\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:\\\/\\\/www.devopsconsulting.in\\\/blog\\\/example-of-laravel-collection-sort-by-date\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.devopsconsulting.in\\\/blog\\\/example-of-laravel-collection-sort-by-date\\\/\"},\"author\":{\"name\":\"Abhishek Singh\",\"@id\":\"https:\\\/\\\/www.devopsconsulting.in\\\/blog\\\/#\\\/schema\\\/person\\\/fc397ba8be42f9fdd53450edfc73006f\"},\"headline\":\"Example of Laravel Collection Sort By Date\",\"datePublished\":\"2023-11-22T06:43:36+00:00\",\"dateModified\":\"2024-10-01T10:28:39+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.devopsconsulting.in\\\/blog\\\/example-of-laravel-collection-sort-by-date\\\/\"},\"wordCount\":211,\"commentCount\":0,\"keywords\":[\"Ascending Order Sort\",\"Collection Sorting\",\"Data Sorting in Laravel\",\"Date Sorting in Laravel\",\"Descending Order Sort\",\"Eloquent Collection Sorting\",\"Example of Laravel Collection Sort By Date\",\"Laravel Collection\",\"Laravel Collection Example\",\"Laravel Collection Methods\",\"Laravel Collection Sort By Date\",\"Laravel sortBy Function\",\"Laravel Sorting Example\",\"PHP Laravel Collections\",\"Sort By Date in laravel\",\"sortBy\",\"sortByDesc\",\"Sorting by Date\"],\"articleSection\":[\"Laravel\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.devopsconsulting.in\\\/blog\\\/example-of-laravel-collection-sort-by-date\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.devopsconsulting.in\\\/blog\\\/example-of-laravel-collection-sort-by-date\\\/\",\"url\":\"https:\\\/\\\/www.devopsconsulting.in\\\/blog\\\/example-of-laravel-collection-sort-by-date\\\/\",\"name\":\"Example of Laravel Collection Sort By Date - DevOps Consulting\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.devopsconsulting.in\\\/blog\\\/#website\"},\"datePublished\":\"2023-11-22T06:43:36+00:00\",\"dateModified\":\"2024-10-01T10:28:39+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.devopsconsulting.in\\\/blog\\\/#\\\/schema\\\/person\\\/fc397ba8be42f9fdd53450edfc73006f\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.devopsconsulting.in\\\/blog\\\/example-of-laravel-collection-sort-by-date\\\/\"]}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.devopsconsulting.in\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.devopsconsulting.in\\\/blog\\\/\",\"name\":\"DevOps Consulting\",\"description\":\"DevOps Consulting | SRE Consulting | DevSecOps Consulting | MLOps Consulting\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.devopsconsulting.in\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.devopsconsulting.in\\\/blog\\\/#\\\/schema\\\/person\\\/fc397ba8be42f9fdd53450edfc73006f\",\"name\":\"Abhishek Singh\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/790feefe779852cdf344ca7318bf6c13832223c9b3c6bf4d217658412041026d?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/790feefe779852cdf344ca7318bf6c13832223c9b3c6bf4d217658412041026d?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/790feefe779852cdf344ca7318bf6c13832223c9b3c6bf4d217658412041026d?s=96&d=mm&r=g\",\"caption\":\"Abhishek Singh\"},\"description\":\"I\u2019m Abhishek, a DevOps, SRE, DevSecOps, and Cloud expert with a passion for sharing knowledge and real-world experiences. I\u2019ve had the opportunity to work with Cotocus and continue to contribute to multiple platforms where I share insights across different domains: \u2022 DevOps School \u2013 Tech blogs and tutorials \u2022 Holiday Landmark \u2013 Travel stories and guides \u2022 Stocks Mantra \u2013 Stock market strategies and tips \u2022 My Medic Plus \u2013 Health and fitness guidance \u2022 TrueReviewNow \u2013 Honest product reviews \u2022 Wizbrand \u2013 SEO and digital tools for businesses I\u2019m also exploring the fascinating world of Quantum Computing.\",\"url\":\"https:\\\/\\\/www.devopsconsulting.in\\\/blog\\\/author\\\/abhishek\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Example of Laravel Collection Sort By Date - DevOps Consulting","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:\/\/www.devopsconsulting.in\/blog\/example-of-laravel-collection-sort-by-date\/","og_locale":"en_US","og_type":"article","og_title":"Example of Laravel Collection Sort By Date - DevOps Consulting","og_description":"Here&#8217;s a detailed tutorial on how to sort a Laravel Collection by date with examples. Example of Laravel Collection Sort By Date In Laravel, you can easily...","og_url":"https:\/\/www.devopsconsulting.in\/blog\/example-of-laravel-collection-sort-by-date\/","og_site_name":"DevOps Consulting","article_published_time":"2023-11-22T06:43:36+00:00","article_modified_time":"2024-10-01T10:28:39+00:00","author":"Abhishek Singh","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Abhishek Singh","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.devopsconsulting.in\/blog\/example-of-laravel-collection-sort-by-date\/#article","isPartOf":{"@id":"https:\/\/www.devopsconsulting.in\/blog\/example-of-laravel-collection-sort-by-date\/"},"author":{"name":"Abhishek Singh","@id":"https:\/\/www.devopsconsulting.in\/blog\/#\/schema\/person\/fc397ba8be42f9fdd53450edfc73006f"},"headline":"Example of Laravel Collection Sort By Date","datePublished":"2023-11-22T06:43:36+00:00","dateModified":"2024-10-01T10:28:39+00:00","mainEntityOfPage":{"@id":"https:\/\/www.devopsconsulting.in\/blog\/example-of-laravel-collection-sort-by-date\/"},"wordCount":211,"commentCount":0,"keywords":["Ascending Order Sort","Collection Sorting","Data Sorting in Laravel","Date Sorting in Laravel","Descending Order Sort","Eloquent Collection Sorting","Example of Laravel Collection Sort By Date","Laravel Collection","Laravel Collection Example","Laravel Collection Methods","Laravel Collection Sort By Date","Laravel sortBy Function","Laravel Sorting Example","PHP Laravel Collections","Sort By Date in laravel","sortBy","sortByDesc","Sorting by Date"],"articleSection":["Laravel"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.devopsconsulting.in\/blog\/example-of-laravel-collection-sort-by-date\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.devopsconsulting.in\/blog\/example-of-laravel-collection-sort-by-date\/","url":"https:\/\/www.devopsconsulting.in\/blog\/example-of-laravel-collection-sort-by-date\/","name":"Example of Laravel Collection Sort By Date - DevOps Consulting","isPartOf":{"@id":"https:\/\/www.devopsconsulting.in\/blog\/#website"},"datePublished":"2023-11-22T06:43:36+00:00","dateModified":"2024-10-01T10:28:39+00:00","author":{"@id":"https:\/\/www.devopsconsulting.in\/blog\/#\/schema\/person\/fc397ba8be42f9fdd53450edfc73006f"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.devopsconsulting.in\/blog\/example-of-laravel-collection-sort-by-date\/"]}]},{"@type":"WebSite","@id":"https:\/\/www.devopsconsulting.in\/blog\/#website","url":"https:\/\/www.devopsconsulting.in\/blog\/","name":"DevOps Consulting","description":"DevOps Consulting | SRE Consulting | DevSecOps Consulting | MLOps Consulting","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.devopsconsulting.in\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.devopsconsulting.in\/blog\/#\/schema\/person\/fc397ba8be42f9fdd53450edfc73006f","name":"Abhishek Singh","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/790feefe779852cdf344ca7318bf6c13832223c9b3c6bf4d217658412041026d?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/790feefe779852cdf344ca7318bf6c13832223c9b3c6bf4d217658412041026d?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/790feefe779852cdf344ca7318bf6c13832223c9b3c6bf4d217658412041026d?s=96&d=mm&r=g","caption":"Abhishek Singh"},"description":"I\u2019m Abhishek, a DevOps, SRE, DevSecOps, and Cloud expert with a passion for sharing knowledge and real-world experiences. I\u2019ve had the opportunity to work with Cotocus and continue to contribute to multiple platforms where I share insights across different domains: \u2022 DevOps School \u2013 Tech blogs and tutorials \u2022 Holiday Landmark \u2013 Travel stories and guides \u2022 Stocks Mantra \u2013 Stock market strategies and tips \u2022 My Medic Plus \u2013 Health and fitness guidance \u2022 TrueReviewNow \u2013 Honest product reviews \u2022 Wizbrand \u2013 SEO and digital tools for businesses I\u2019m also exploring the fascinating world of Quantum Computing.","url":"https:\/\/www.devopsconsulting.in\/blog\/author\/abhishek\/"}]}},"_links":{"self":[{"href":"https:\/\/www.devopsconsulting.in\/blog\/wp-json\/wp\/v2\/posts\/925","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsconsulting.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsconsulting.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsconsulting.in\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsconsulting.in\/blog\/wp-json\/wp\/v2\/comments?post=925"}],"version-history":[{"count":3,"href":"https:\/\/www.devopsconsulting.in\/blog\/wp-json\/wp\/v2\/posts\/925\/revisions"}],"predecessor-version":[{"id":1247,"href":"https:\/\/www.devopsconsulting.in\/blog\/wp-json\/wp\/v2\/posts\/925\/revisions\/1247"}],"wp:attachment":[{"href":"https:\/\/www.devopsconsulting.in\/blog\/wp-json\/wp\/v2\/media?parent=925"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsconsulting.in\/blog\/wp-json\/wp\/v2\/categories?post=925"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsconsulting.in\/blog\/wp-json\/wp\/v2\/tags?post=925"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}