title: Swift - How to use a class type as a parameter / variable
body: <p>I'm trying to create a somewhat generic function to sort file URLs based the value of an attribute</p>
  
  <p>My goal is to:</p>
  
  <p>1) Pass the URL and some parameters (Including the type) to a function. 
  which will then loop through the file's attributes</p>
  
  <p>2) Add the matching file attribute and the URL to an array of tuples</p>
  
  <p>3) Sort the tuples by the value of the found attribute </p>
  
  <p>4) Return the sorted array and display the items in sorted order</p>
  
  <p>I believe that I need to pass the type of the attribute into the sorting function so I'm able to set it in the tuple since I'm unable to sort with "Any" but I'm unsure of how to do that</p>
  
  <p>I'm okay with passing anything into the sorting function and constructing or deconstructing the value I need in the sorting function since that will be predefined depending upon what action is selected by the user</p>
  
  <pre><code>//Initial implementation to be later tied to IBActions and simplified   
  
  func sortFiles(sortByKeyString : String, fileURLArray : [URL]) -&gt; [URL] 
  {
  
          switch sortByKeyString {
              case "date-created-DESC":
                  let fileAttributeKeyString : String = "creationDate"
                  let isSortOrderDesc = true
                  let objectTypeString : String = NSDate.className()
                  let sortedFileURLArray = sortFileArrayByType(fileAttributeKeyString: fileAttributeKeyString, fileURLArray: fileURLArray, type: objectTypeString, isSortOrderDesc : isSortOrderDesc)
                  return sortedFileURLArray
              default:
                  return fileURLArray
          }
      }
  
  </code></pre>
  
  <pre><code>//Generic function to get a files attributes from a URL by requested 
  type
  
      func sortFileArrayByType(fileAttributeKeyString : String, fileURLArray : [URL], type: String, isSortOrderDesc : Bool) -&gt; [URL] {
          let fileManager = FileManager.default
          let attributeToLookFor : FileAttributeKey = FileAttributeKey.init(rawValue: fileAttributeKeyString)
          var tupleArrayWithURLandAttribute : [(url: URL, attribute: *Any*)]? = nil
  
          for url in fileURLArray {
              do {
                  let attributes = try fileManager.attributesOfItem(atPath: url.path)
                  for (key, value) in attributes {
                      if key.rawValue == fileAttributeKeyString {
                          tupleArrayWithURLandAttribute?.append((url: url, attribute: value))
                      }
  
                  }
                  let sortedTupleArrayWithURLandAttribute = tupleArrayWithURLandAttribute?.sorted(by: { $0.attribute &lt; $1.attribute)})
                  // Need to Sort dictionary into array
                  return sortedTupleArrayWithURLandAttribute
              } catch {
                  return fileURLArray
              }
          }
      }
  </code></pre>