 }
 
 func printTagsArray() {
-
-    let names = allTags.keys.sort( { $0.lowercaseString < $1.lowercaseString } )
-
+    
+    let names = allTags.keys.sorted( by: { $0.lowercased() < $1.lowercased() } )
+    
     let varName = "var tagNameList = []string{"
-
+    
     print("\(varName)")
     for name in names {
         print("\"\(name)\",", terminator:" ")
     }
     print("}")
-
+    
 }
 
 func printTagCountVar() {
     print("var tagCountDict = map[string]int{")
-    for (key, value) in allTags.sort({ $0.0.lowercaseString < $1.0.lowercaseString }) {
+    for (key, value) in allTags.sorted(by: { $0.0.lowercased() < $1.0.lowercased() }) {
         //    let count = allTags[t]
         print("\"\(key)\": \(value),")
     }
     print("}")
 }
 
-database.sortInPlace({ (rec1, rec2) -> Bool in
+database.sort(by: { (rec1, rec2) -> Bool in
     if rec1.groupNum < rec2.groupNum {
         return true
     } else if rec1.groupNum == rec2.groupNum {
-
+        
         if rec1.date > rec2.date { // most recent first
             return true
         } else if rec1.date == rec2.date {


func readLines(filePath: String) -> [String] {
    //    var stream:NSStream = NSStream

    var allLines:[String] = []

    if let fileManager =  NSFileManager.defaultManager() as NSFileManager! {
        let fileExists = fileManager.fileExistsAtPath(filePath)
        if fileExists {
            let data:NSData? = fileManager.contentsAtPath(filePath)

            let aString = NSString(data: data!, encoding: NSUTF8StringEncoding)

            let str1:String = aString as! String
            allLines = str1.componentsSeparatedByString("\n")

        } else {
            print("file not found: \(filePath)")
        }


    }
    return allLines

}

func writeGoVar(groupNum: Int, url: String, name: String,
    source: String, githubName:String,
    tags: String, image: String, date: Int,
     isPaywall: Int, apisUsed: String,
      recType: Int, key: Int) {
        var allTagStr = ""

        let lowerTitle = name.lowercaseString
        let tagList = tags.componentsSeparatedByString(":")
        for t in tagList {
            if t != "" {

                allTagStr += "\"\(t)\","
            }
      }
        print("{\(groupNum), \"\(url)\",\"\(name)\",\"\(lowerTitle)\" ,\"\(source)\", \"\(githubName)\", []string{\(allTagStr)}, \"\(image)\", \(date), \(isPaywall), \"\(apisUsed)\", \(recType), \(key)  },")
}

func deriveSource(url: String) -> (String, String) {

    var source = "n/a"
    var githubName = ""
    var F:[String] // Translated from Perl?
    let F0 = url.componentsSeparatedByString("//")
    let url0:String = F0[1]
    F = url0.componentsSeparatedByString("/")
    if url.rangeOfString("github.com") != nil {
        source = "github.com/" + F[1]
        // githubName = url0.stringByReplacingOccurrencesOfString("github.com/", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)
        githubName = F.last!

                if (date == nil) {
                    date = 20010101
                }
                
                var isPaywall:Int? = Int(aLine[6])
                
                if isPaywall == nil {
                    isPaywall = 0
                }
                
                let apisUsed = aLine[7]
                
                var recType:Int? = Int(aLine[8])
                
                if recType == nil {
                    recType = 0
                }
                
                var recordKey:Int? = Int(aLine[9])
                
                if recordKey == nil {
                    recordKey = 0
                }
                
                let rec = UrlRecord(id: i, groupNum: groupNum, source: source, url: url, title: title, githubName: githubName, tags: tags, image: image, date: date!, isPaywall: isPaywall!, apisUsed: apisUsed, recType: recType!, key:recordKey!)
                database.append(rec)
                
                
            } else {
                print("Error: Invalid Group: \(line)")
            }
