Thank you for your reply. Yes and no. I've figured out that I can do it by having an additional Date attribute. So I've started with a new Xcode project by having a new Date attribute named createdAt. And code goes as follows.
import SwiftUI
struct ContentView: View {
@Environment(\.managedObjectContext) var managedObject
@FetchRequest(sortDescriptors: [NSSortDescriptor(keyPath: \Student.createdAt, ascending: true)]) var students: FetchedResults<Student>
var body: some View {
VStack {
List(students) { student in
Text(student.name ?? "")
}
Button {
let firstNames = ...
let lastNames = ...
if let selectedFirstName = firstNames.randomElement(), let selectedLastName = lastNames.randomElement() {
let newStudent = Student(context: managedObject)
newStudent.id = UUID()
newStudent.name = "\(selectedFirstName) \(selectedLastName)"
newStudent.createdAt = Date() // <<<<<<<<<<<<<<<<<<<<<<
try? managedObject.save()
}
} label: {
Text("Add")
}
}
}
}
, which takes care of the sort issue. As for the older project shown above, I wonder if Core Data has a simple mechanism in reordering the records when the entity does not include an additional Date attribute? That was my initial question.