Search code examples
swiftsortingcore-dataswiftuinsfetchrequest

Sort FetchRequest by last item added to Core data


I'm trying to sort my core data entities by last item added but I don't know how to achieve it with my code

@FetchRequest(entity: CoffeeItems.entity(), sortDescriptors: [NSSortDescriptor(key: "title", ascending: true)]) var coffeeItem: FetchedResults<CoffeeItems>

Solution

  • The above code is correct. But the problem is you have been sorting based on the title of your objects.

    You can add another variable to your object as timeAdded, where you can store your object's creation time. And you can fetch your objects based on timeAdded.