I have the below structs in my project:
struct templateScheduleResponse: Decodable {
let templateScheduleTypes: [scheduleTemplateType]
}
struct scheduleTemplateType: Decodable {
let templateScheduleNames: [templateScheduleName]?
let templateTypeId: String?
let templateTypeName: String
}
struct templateScheduleName: Decodable {
let templateNameId: String?
let templateSchedName: String
}
and I want to sort it so that the array of templateScheduleName is sorted by templateSchedName value.
I'm completely stuck on how to do this.
Any pointers, please? (I'm just started learning Structs, if you couldn't tell!)
Thanks!
UPDATE: Here is my JSON data:
{
templateScheduleTypes = (
{
templateScheduleNames = (
{
templateNameId = "fad562bc-4510-49ea-b841-37a825a2f835";
templateSchedName = "Daily_Intensive";
},
{
templateNameId = "fdeeb79f-6321-4ff6-b1f0-8272a018e73b";
templateSchedName = "Weekly_Full_Log_Directories";
},
{
templateNameId = "84f9800f-da18-44b8-822f-830069dcc594";
templateSchedName = "Weekly_Full";
},
{
templateNameId = "47a6f050-13d7-4bf6-b5db-ab53e0a3aa54";
templateSchedName = "Weekly_Full_Catalog_Two_Weeks";
},
{
templateNameId = "b8ef9577-e871-4d79-8d3a-cfe958c0c3aa";
templateSchedName = "Weekly_Full_Over_WAN";
},
{
templateNameId = "8d507f52-0d74-404e-ad0d-76e6a7a94287";
templateSchedName = "Monthly_Full";
}
);
templateTypeId = "4e73b9ea-71d0-4abd-83c6-7d7b6d45641b";
templateTypeName = datalist;
},
{
templateScheduleNames = (
{
templateNameId = "39386552-45a5-4470-b152-7be00583e767";
templateSchedName = "Scheduled_Exchange_Server";
}
);
templateTypeId = "a7c28240-c187-4f86-818c-efd86fb26c7d";
templateTypeName = MSESE;
},
{
templateScheduleNames = (
{
templateNameId = "0037846c-d1fe-4c8f-8eec-c62681a12a57";
templateSchedName = "Scheduled_Exchange_Single_Mailbox";
}
);
templateTypeId = "9e06f06a-11dc-44b8-97a0-68bd0b45a07a";
templateTypeName = Mailbox;
}
);
}
2 Answers 2
You can do an ad hoc sort without having to conform to Comparable. If x is some variable of type templateScheduleType:
x.templateScheduleNames.sorted(by: { (lhs, rhs) -> Bool in
return lhs.templateSchedName < rhs.templateSchedName
})
If you want to ensure the array is sorted in place at construction time, define an init method on scheduleTemplateType just as you would on a class:
init(scheduleNames: [templateScheduleName], typeID:String?, typeName:String) {
self.templateScheduleNames = scheduleNames.sorted(by: { (lhs, rhs) -> Bool in
return lhs.templateSchedName < rhs.templateSchedName
})
self.templateTypeId = typeID
self.templateTypeName = typeName
}
-
Ah! Thanks. This looks more promising with the init method. But the sort order still doesn't seem right, it comes out like: imgur.com/a/WJrN7FlatDog– FlatDog2017年08月01日 15:22:29 +00:00Commented Aug 1, 2017 at 15:22
First of all struct names should begin with an uppercase char.
To answer your question, you need to make TemplateScheduleName
struct TemplateScheduleName: Decodable {
let templateNameId: String?
let templateSchedName: String
}
conform to Comparable
extension TemplateScheduleName: Comparable {
static func ==(lhs: TemplateScheduleName, rhs: TemplateScheduleName) -> Bool {
return lhs.templateSchedName == rhs.templateSchedName
}
public static func <(lhs: TemplateScheduleName, rhs: TemplateScheduleName) -> Bool {
return lhs.templateSchedName < rhs.templateSchedName
}
}
Now given
let list : [TemplateScheduleName] = []
you can easily sort it
let sortedList = list.sorted()
-
Thanks, but if I have a list of TemplateScheduleResponse (the top struct), how do I sort the oder within that of the schedule names?FlatDog– FlatDog2017年07月31日 13:32:23 +00:00Commented Jul 31, 2017 at 13:32
-
I would not conform to comparable.
sorted
can just take in a closure anyway, and there's no reason to infer that this sorting criteria can define equatabilityConnor Neville– Connor Neville2017年07月31日 18:15:07 +00:00Commented Jul 31, 2017 at 18:15