Commit e2c7841e authored by Ilya Prokhorov's avatar Ilya Prokhorov

lexiCompare algorithm implementation

parent 79ce0cac
func lexiCompare(_ lhs: String, _ rhs: String) -> Int {
let lhs = Array(lhs)
let rhs = Array(rhs)
print(lhs)
for i in 0...lhs.count {
if i >= rhs.count {
return 0
}
let lhsSymbol = lhs[i]
let rhsSymbol = rhs[i]
if lhsSymbol < rhsSymbol {
return -1
}
else if lhsSymbol > rhsSymbol {
return 1
}
else {
continue
}
}
return 0
}
let lhs = "aaa"
let rhs = "ccc"
let result = lexiCompare(lhs, rhs)
print(result)
/home/demensdeum/Apps/swift5/usr/bin/swift lexicographicSort.swift
import Foundation
func lexiCompare(_ lhs: String, _ rhs: String) -> Int {
let lhs = Array(lhs)
let rhs = Array(rhs)
for i in 0...lhs.count {
if i >= rhs.count {
return 0
}
let lhsSymbol = lhs[i]
let rhsSymbol = rhs[i]
if lhsSymbol < rhsSymbol {
return -1
}
else if lhsSymbol > rhsSymbol {
return 1
}
else {
continue
}
}
return 0
}
let url = URL(fileURLWithPath: "../../resources/whitelist.json")
let data = try? Data(contentsOf: url)
var json = try? JSONDecoder().decode([String : [String]].self, from: data!)
......@@ -7,7 +32,8 @@ var json = try? JSONDecoder().decode([String : [String]].self, from: data!)
let whiteList = json!["whiteList"]!
print(whiteList)
var whiteListSorted = whiteList.sorted(by: { $0.compare($1).rawValue < 0 });
var whiteListSorted = whiteList.sorted(by: { lexiCompare($0, $1) < 0 });
//var whiteListSorted = whiteList.sorted(by: { $0.compare($1).rawValue < 0 });
print(whiteListSorted)
json!["whiteListSorted"] = whiteListSorted
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment