Say I want to rank users by compatibility, each user has a score in a different attribute (all 1-5):
Smoker: 1
Cleanliness: 3
Night owl: 2
I would like each user array to contain these attributes, I'll have all these users in a multi-dimensional array.
Is there a simple way for me to sort my multi-dimensional array by user compatibility to a given user?
For example, I have User1: smoker = 1. Cleanliness = 3. Night owl = 2.
whom I would like to sort all other users for compatibility with.
I also have these 2 other users:
User2: smoker = 5. Cleanliness = 1. Night owl = 4
User3: smoker = 1. Cleanliness = 3. Night owl = 2.
Since User3
is identical to User1
(who I wish to rate compatibility with), my array should be sorted as follows: User1
, User3
, User2
. The difficult part seems to be sorting them by closeness, rather than in asc or desc order, say a user had smoker = 3
I'd either want the next user in the sorted array to have smoker = 2
or smoker = 4
(assuming no other use had 3). I'd need a way to gauge closeness overall, throughout each attribute for each user.
Is there a simple way to do this? A general method would be great, a PHP example would be even better.
in such problems you need to weight each part (like smoker
, Cleanliness
, night owl
, day's chicken
, ...) based on a story, or logic!
for example, I know in my application a smoker
is more important than a Cleanliness
as 10 fold, therefore, if I rank a Cleanliness
like 5, I'd rank smoker
as 50!
so with User1: smoker = 1. Cleanliness = 3. Night owl = 2
I'd have
1*50 + 3*5 + 2*1 = 67
is such rankings logical?? it depends! as I mentioned earlier...
with a weighted scenario, you can calculate each row's value and sort your collection easily.