Sorting queries oldest to newest, newer than today, then oldest to newest, older than today

Hello, I’m used to using SQL queries, but it’s been a few years for that as well.

I have a (forgive my terms, new to swift). Entity with only a few attributes.

One being ‘date’. I’d like to create the output of the order of any day that’s today or in the future, in the order of closest at the top, and furthest at the bottom, with any date that’s older than today, in the same order, after the last future date.

before that join, though, it should sort alphabetically on ‘eventName’ in case there are multiple events on the same day.

Hopefully that makes sense.

I think in SQL I’d do 2 queries using today’s date and a join or union. grab both as one, then pass it to my app.

But in here, I’m new to this. I did get it to sort by date, but it puts past dates at very top.

thanks for any help. I’ll keep messing with it as well.

or maybe get the data then pass it to an array then use the array to build the table?
or make 2 arrays from 2 queries and join them after and use that to build the table?

I’m only working with 3 attributes, and maybe a dozen rows, so efficiency is nice, but nothing will break from the amount of data.

func getAllItems() {
        do {
            let request = DazeData.fetchRequest() as NSFetchRequest<DazeData>
            let sort = NSSortDescriptor(key: "eventDate", ascending: true)
            request.sortDescriptors = [sort]
            dazeTable = try context.fetch(request)
            
            DispatchQueue.main.async {
                self.tableView.reloadData()
            }
        }
        catch {
            // error handler go here
        }
    }

GOOD NEWS EVERYONE (Farnsworth voice from Futurama)

I have the alphabetical started. I’ll now start playing with seeing if I can sort dates separated by today.


            let sort1 = NSSortDescriptor(key: "eventDate", ascending: true)
            let sort2 = NSSortDescriptor(key: "eventName", ascending: true)
            
            
            request.sortDescriptors = [sort1, sort2]

1 Like

If you want the most recent date to be first then set ascending: false in your sort1

that is correct, but I’m trying to take one list and make it two (then display as one) to make it easier to glance at what’s important. I’ll try a few things today since I’m stuck on getting my date picker to work at the moment.

thanks

Just remember that Core Data is not a relational database like SQL, with tables and joins. Core Data may or may not store its data in an SQLite database, but that is an implementation detail that shouldn’t be of concern. So don’t get too hung up on trying to treat it like one.

True, you guys have it way too easy today.I actually had to setup the server and SQL install back in the day.

I think I figured out how I’ll attempt it. I use a function to run the query and return the data to build the table. I’ll just run the function 2x, passing it first a value of ‘upcoming’ and then ‘past’ which will run different queries accordingly, getting the data I want, in the order I want.

Hopefully that works, I can try it out later today.

thanks

Looks like I need to use Predicates. Using today’s day as the cut off

Find all matches on attribute eventDate >= Date()
(add to display table)
Find all matches Yesterday and past eventDate < Date()
(Add to display table).