Skip to content

Instantly share code, notes, and snippets.

@gamako
Last active December 20, 2018 08:45
Show Gist options
  • Save gamako/6c8d56490049be9f4fa8dec1626bcb06 to your computer and use it in GitHub Desktop.
Save gamako/6c8d56490049be9f4fa8dec1626bcb06 to your computer and use it in GitHub Desktop.
swift version string compare
import Foundation
ComparisonResult.orderedAscending.rawValue // -1
ComparisonResult.orderedSame.rawValue // 0
ComparisonResult.orderedDescending.rawValue // 1
"1.0.0".compare("1.0.1", options: .numeric, range: nil, locale: nil).rawValue // -1 (orderedAscending)
"1.0.0".compare("1.0.0", options: .numeric, range: nil, locale: nil).rawValue // 0 (orderedSame)
"1.0.0".compare("0.9.9", options: .numeric, range: nil, locale: nil).rawValue // 1 (orderedDescending)
"1.0.0".compare("1.0.1", options: .numeric, range: nil, locale: nil).rawValue // -1 (orderedAscending)
"1.0.1".compare("1.0.1", options: .numeric, range: nil, locale: nil).rawValue // 0 (orderedSame)
"1.0.2".compare("1.0.1", options: .numeric, range: nil, locale: nil).rawValue // 1 (orderedDescending)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment