I have a strange sorting issue with my models.
Given the following:
- I have a model called Cv, which has many occupations and skills, and is linked to a country.
- I also have a model called Vacancy, which has one occupation and many skills, and is also linked to a country.
In my controller, I have the following lines of code:
@occupations = Occupation.find params[:occupation_ids].split(' ')
@skills = Skill.find params[:skill_ids].split(' ')
@cv = Cv.find params[:cv_id]
@language = Language.resolve({:code => :en, :name => :en})
@vacancies = Vacancy.joins(:vacancy_skills).where('vacancy_skills.skill_id' => params[:skill_ids]).all.uniq.sort{ |x,y| (x.skills | @skills).length <=> (y.skills | @skills).length }
The idea is that all vacancies are beeing sorted by their relevance, which is calculated by the length of the union between the selected skills (@skils) and the skills of the vacancy in question.
When I run the code, I receive all vacancies, ordered by country and sorted by relevance, but I don’t want them ordered by country, instead I want them all mixed but sorted by relevance.
The current output is :
- vacancy 1, 100% relevance, united kingdom
- vacancy 2, 90% relevance, united kingdom
- vacancy 3, 10% relevance, united kingdom
- vacancy 4, 90% relevance, france
- vacancy 5, 70% relevance, fance
The desired output should be like this:
- vacancy 1, 100% relevance, united kingdom
- vacancy 2, 90% relevance, united kingdom
- vacancy 4, 90% relevance, france
- vacancy 5, 70% relevance, france
- vacancy 3, 10% relevance, united kingdom
What am I supposed to change/do to get it like that?
problem was in the sort algorithm.
I used the | operator instead of the & operator.