Advertisement
Guest User

Untitled

a guest
Jul 20th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 0.66 KB | None | 0 0
  1. struct Account {
  2.     let currencyCode: String
  3.     let openDate: Date
  4. }
  5.  
  6. let currencyCodes = ["RUR": 0, "USD": 1, "EUR": 2]
  7.  
  8. func sortedAccounts(_ accounts: [Account]) -> [Account] {
  9.     return accounts
  10.         .sorted { first, second in
  11.            
  12.             switch first.currencyCode == second.currencyCode {
  13.             case true:
  14.                 return first.openDate < second.openDate
  15.             case false:
  16.                 let secondCurrency = currencyCodes[second.currencyCode] ?? Int.min
  17.                 let firstCurrency = currencyCodes[ first.currencyCode] ?? Int.max
  18.                 return firstCurrency < secondCurrency
  19.             }
  20.         }
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement