October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run ScanOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

How to Group and Display a List by Date in SwiftUI

Turn dated records into ordered SwiftUI List sections using calendar-aware day keys, localized headers, and stable row IDs.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Group records by a calendar-aware day key, sort the resulting groups, and display each group in a SwiftUI Section inside a List. Keep the date used for grouping separate from the localized date string shown to readers, and use stable model IDs for rows.

Choose what “same day” means

A Date represents a point in time, not a calendar day. Decide which calendar and time zone define a day for your app before grouping records. For a personal activity feed, the viewer’s current calendar is often appropriate. For a shared schedule or business record, use the calendar and time zone specified by the domain rather than allowing each viewer’s settings to shift entries between days.

As an Amazon Associate I earn from qualifying purchases.

For calendar-day grouping, use Calendar.startOfDay(for:) to turn each timestamp into a day key. This avoids treating two timestamps on the same day as different groups, and avoids using a formatted label—which can vary by locale—as a data key. Apple documents calendar date operations in Calendar.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Group and order the records

Here is one implementation pattern for entries grouped newest-first, with the newest entry first within each day:

import SwiftUI

struct Entry: Identifiable {
    let id: UUID
    let date: Date
    let title: String
}

struct DayGroup: Identifiable {
    let day: Date
    let entries: [Entry]
    var id: Date { day }
}

func groupedByDay(
    _ entries: [Entry],
    calendar: Calendar = .current
) -> [DayGroup] {
    let grouped = Dictionary(grouping: entries) { entry in
        calendar.startOfDay(for: entry.date)
    }

    return grouped.keys.sorted(by: >).map { day in
        DayGroup(
            day: day,
            entries: grouped[day, default: []].sorted { $0.date > $1.date }
        )
    }
}

The function makes the ordering explicit: dictionary key iteration does not define a timeline’s chronology. Reverse the comparison to < for oldest-first sections and rows, as in a forward-looking agenda. If the source already guarantees the desired row order, you can omit the inner sort.

The example uses each day’s start as a group ID because a day appears only once in the returned array. Keep each entry’s persistent unique ID for its row; an array index is not a reliable identity when data can be inserted, deleted, or reordered. Apple’s Displaying data in lists guide explains how SwiftUI uses identifiable data to track changes.

Display each day as a SwiftUI section

Use a List for the vertical collection, a Section for each date heading, and a nested ForEach for that day’s entries:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
struct EntryList: View {
    let groups: [DayGroup]

    var body: some View {
        List {
            ForEach(groups) { group in
                Section {
                    ForEach(group.entries) { entry in
                        Text(entry.title)
                    }
                } header: {
                    Text(
                        group.day,
                        format: .dateTime.month(.wide).day().year()
                    )
                }
            }
        }
    }
}

The date format is for presentation; it does not determine membership in a group. Date.FormatStyle provides a Swift-friendly way to create localized date output. See Apple’s DateFormatter documentation for date formatting and its reference to Date.FormatStyle.

Choose where grouping belongs

You can compute groups before passing them to the view, or derive them while composing the view. Neither approach is required by SwiftUI; choose based on how the data is used.

Approach Useful when Trade-off
Precompute a [DayGroup] in a model or view-model layer Several screens need the same grouping, or you want to test and reason about the transformation separately from rendering. You must refresh the derived groups when the source records or relevant calendar settings change.
Group inline while building the view The collection is small and static, and keeping the transformation close to the rendering is convenient. Reuse and isolated testing can be less straightforward as the view grows.

For pagination or frequently changing data, ensure each update rebuilds or updates the groups from the current records, then preserve stable entry IDs. If the app lets users change calendar or time-zone settings while the view remains visible, recompute the day keys under the new settings.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Handle empty dates and common grouping mistakes

  • Do not group by a display string. A label is localized presentation, not a dependable calendar key.
  • Do not compare full timestamps for day equality. Different times on one calendar day are still different Date values.
  • Do not silently use UTC for a local-day interface. Entries close to midnight can appear under a different day than users expect.
  • Do not rely on dictionary order. Sort section keys, and sort rows when their source order is not meaningful.
  • Generate empty dates separately if the design needs them. Grouping records creates only groups that contain records. To show dates with no entries, create the desired date range and render empty sections intentionally.

Apple’s SwiftUI list guide describes the view structure: a List can contain sections and rows, with each row represented by a SwiftUI view. The date-key logic and ordering remain application decisions; the example above is a practical pattern, not an Apple-prescribed grouping architecture.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.