I have an array of files. The file conforms to the below File class:
class File {
var name: String?
var type: String?
var timeStamp: Date?
}
let array = [file1,file2,file3,file4]
The type could have values "dir" , "jpg", "png" , "mov"
I now want to sort the array based on type, name and datestamp. First I want to group files of similar types. For example, dir files should come first then the other ones. Now, I want to sort dir type files based on names and the othe file types based on the datestamp.
I was writing this code:
self.directoryContents = self.directoryContents?.sorted(by: { (file1, file2) -> Bool in
if file1.type == file2.type {
return file1.name! > file1.name!
}else {
return file1.timeStamp! > file2.timeStamp!
}
})
I am not able to achieve the desired sorting. Any help would be really appreciated.