<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Armaan Khan</title>
    <description>The latest articles on DEV Community by Armaan Khan (@armaan535kh).</description>
    <link>https://dev.to/armaan535kh</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4139317%2Fd15819f1-d673-4a36-9416-630adf989899.png</url>
      <title>DEV Community: Armaan Khan</title>
      <link>https://dev.to/armaan535kh</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/armaan535kh"/>
    <language>en</language>
    <item>
      <title>How to Group and Display a List by Date in SwiftUI</title>
      <dc:creator>Armaan Khan</dc:creator>
      <pubDate>Wed, 23 Sep 2026 11:21:57 +0000</pubDate>
      <link>https://dev.to/armaan535kh/how-to-group-and-display-a-list-by-date-in-swiftui-4ggh</link>
      <guid>https://dev.to/armaan535kh/how-to-group-and-display-a-list-by-date-in-swiftui-4ggh</guid>
      <description>&lt;h2&gt;
  
  
  Problem I Faced
&lt;/h2&gt;

&lt;p&gt;I was working a gym Mac app for myself, I am new to SwiftUI and Apple ecosystem. I think I might be an almost year learning swift.&lt;/p&gt;

&lt;p&gt;Till now I was just simply working on simple projects. Now I decided atleast I have to solve me daily problem.&lt;/p&gt;

&lt;p&gt;I need to create an app for gym where I can track my progress. &lt;/p&gt;

&lt;p&gt;While creating I got a Issue I want to fetch the workouts and show in the form of the list, but the problem was that I want to show the workouts that I have done according to the Dates.&lt;/p&gt;

&lt;h2&gt;
  
  
  How did I solved
&lt;/h2&gt;

&lt;p&gt;I used Swift Data to store my data locally. It generally stores the data in the flat list. To show my all workout according to date I have to convert flat list into dictioinay.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Converting flat list into Dictionary
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fyy3alxcffwqt8bu1s65c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fyy3alxcffwqt8bu1s65c.png" alt=" " width="800" height="364"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I created a Computed Property to convert the flat list into dictionary.&lt;/p&gt;

&lt;p&gt;Dictionary(grouping: workouts) { ... } is a built-in Swift tool that walks through every item in workouts, one at a time, and asks the closure: "what key should this item live under?"&lt;/p&gt;

&lt;p&gt;{ workout in Calendar.current.startOfDay(for: workout.date) }&lt;/p&gt;

&lt;p&gt;It takes workout.date (which might be Sept 23, 9:03 AM) and strips the time off, giving just Sept 23, 12:00 AM. This is important — without this step, the 9am workout and the 6pm workout would be treated as different keys (since their exact timestamps differ), and they'd never end up grouped together.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Two ForEachs
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F6we2db2g8c12txavyrva.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F6we2db2g8c12txavyrva.png" alt=" " width="800" height="336"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, First ForEach shorts the items in the dictionary recent as first element.&lt;/p&gt;

&lt;p&gt;Dictionaries have no built-in order in Swift, so if you looped over .keys directly, the dates could appear in any random order, and it might change every time. That's why .sorted(by: &amp;gt;) is there — it sorts the dates newest-first (&amp;gt; means descending).&lt;/p&gt;

&lt;p&gt;groupedWorkouts[date] looks up the array of workouts belonging to that day.&lt;/p&gt;

</description>
      <category>swift</category>
      <category>ios</category>
    </item>
  </channel>
</rss>
