There are two parts to this question: getting the Contextual filtering to reduce the results to only the term IDs you specify in the URL, and using Aggregation for counting and sorting the results according to that count.
Contextual filter for Taxonomy term ID
- Create your Views list of nodes with the Taxonomy reference field showing the tags, and configure that field to show all values in the same row
- Add the Contextual filter for your Taxonomy term field or for the Field
Has taxonomy term ID and configure it:
When the filter value is NOT available
Provide default value
Taxonomy term ID from URL
Load default filter from node page, that's good for related taxonomy blocks
Filter to items that share any term
When the filter value IS available or a default is provided
Specify validation criteria
Taxonomy term ID
One or more IDs separated by , or +
Allow multiple values
Test this by entering the term ID (number) into the Views Preview field. You should see the results reduce to show nodes tagged with that term. Also test for more than one term, enter, for example,
3+5 to show nodes that are tagged with either terms 3 or 5, or
3,5 to show nodes tagged with both terms 3 and 5.
If you want to use term names instead of term ID numbers, then instead of using the taxonomy term reference field as the Contextual filter, you'll have to first add a Relationship for that field, then you will have the Taxonomy
Name Contextual filter available. You configure it similarly as above, be careful about case options. You test it by entering the term names into Preview:
Count with Aggregation
- Turn Aggregation on in the third column of Views settings
Click on Aggregation settings for the Taxonomy field (from step 1. above) and change the Aggregation type to Count DISTINCT.
This field will now show the count how many terms are shared.
- Add the Sort criteria for your Taxonomy terms field, and set its Aggregation to
Count DISTINCT and you'll probably want to sort descending. You should have no other sorting added except this. You will see the nodes sorted by the number of tags shared with the contextual filter.
Test this the same way as above, entering term IDs, or term names if that's what you're using.
In the end you'll have to test in a real page to check that the Contextual filter has been configured properly. Build and test in steps, don't first build everything and only test because if there's something wrong it will be more difficult to figure out what and you'll have to start from scratch step by step anyway.