I'm implementing a search algorithm, and I want to show the results from most relevant to least relevant. I have an array of profiles from which I want to get an OrderedSet
of the profiles' IDs, in order of relevance. My classifications, from most relevant to least relevant, are:
completeMatch
firstWordMatch
notFirstWordMatch
firstWordContains
notFirstWordContains
socialContains
I can call profiles.reduce(into: // Intermediate model) { ... }
on the profiles to do this. However, I'm wondering what my intermediate model, which I would then convert into the OrderedSet
of profile IDs, should be:
- An array of tuples composed of the profile ID and the classification as an enum:
enum SearchResult: Int {
case completeMatch = 0
case firstWordMatch = 1
case notFirstWordMatch = 2
case firstWordContains = 3
case notFirstWordContains = 4
case socialContains = 5
}
let results: [(Profile.ID, SearchResult)] = profiles
.reduce(into: [(SentProfile.ID, SearchResult)]()) { /* ... */ }
searchResults: OrderedSet<Profile.ID> = .init(
results
.sorted { lhs, rhs in
let (_, lhsClassification) = lhs
let (_, rhsClassification) = rhs
return lhsClassification.rawValue < rhsClassification.rawValue
}
.map { profileID, _ in profileID }
)
- Or a struct of each classification as independent ordered sets:
struct SearchResult {
var completeMatch: OrderedSet<SentProfile.ID> = []
var firstWordMatch: OrderedSet<SentProfile.ID> = []
var notFirstWordMatch: OrderedSet<SentProfile.ID> = []
var firstWordContains: OrderedSet<SentProfile.ID> = []
var notFirstWordContains: OrderedSet<SentProfile.ID> = []
var socialContains: OrderedSet<SentProfile.ID> = []
func joined() -> OrderedSet<SentProfile.ID> {
return completeMatch
.union(completeMatch)
.union(firstWordMatch)
.union(notFirstWordMatch)
.union(firstWordContains)
.union(notFirstWordContains)
.union(socialContains)
}
}
let results: SearchResult = profiles
.reduce(into: SearchResult()) { /* ... */ }
searchResults = searchResults.joined()
My main concern is time complexity, and I can't quite seem to decide which is better between the two.