swift - What are some ways I can optimize this special cased closure? -


i want use sorted method on array.

public func sorted(by areinincreasingorder: (self.iterator.element, self.iterator.element) -> bool) -> [self.iterator.element]

and can see takes function (which takes 2 elements , returns bool), , returns an array of element (sorted)

where getting stuck, trying pass anonymous function (closure), using $0 , $1 arguments

i want add special case, specific key ("module")

and want access property on $0 , $1

but means, ostensibly, have cast arguments, can access properties on arguments

and make things worse, because arguments either string or node, have repeated code (the switch statement)

so anyways, of swift veterans see things make better?

return self.sorted(by: {             descriptor in sortdescriptors {                 if descriptor.key == "module" {                     if let firstnode = $0 as? node {                         if let secondnode = $1 as? node {                             let firstmodule = firstnode.module?.name ?? ""                             let secondmodule = secondnode.module?.name ?? ""                             let newdescriptor = nssortdescriptor(key: nil, ascending: descriptor.ascending, selector: descriptor.selector)                             switch newdescriptor.compare(firstmodule, to: secondmodule) {                             case .orderedascending:                                 return true                             case .ordereddescending:                                 return false                             case .orderedsame:                                 continue                             }                         }                     }                 }                 switch descriptor.compare($0, to: $1) {                 case .orderedascending:                     return true                 case .ordereddescending:                     return false                 case .orderedsame:                     continue                 }             }             return false         }) } 


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -