Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    A Bit of a problem figuring how to SORT Array

    I have a 2 arrays that I need them to work together. I have the code like follows!



    Code:
    while($member = $DB->fetch_array($result, MYSQL_ASSOC))
    {
    
    // Return a list of members that live near user
    $close_people[$member['member_id']] = $member;
    
    // I also want to check how similar their profiles are, this function compares two profiles and returns a score between 0 and 100
    $similarity[$member['member_id']] = similarity($_SESSION['member_id'], $member['member_id']);
    }
    
    // Now I have 2 lists a list of people by zip code and a list of each of those members similarity score.
    
    // I want the members with the highest scores to lowest, high scores first on the list so I use this function to sort.  
    
    arsort($similarity);
    
    // Now I would like to sort the $close_people list based on the $similarity scores high to low
    
    ????
    any sugestions? THanks I am pretty new and would Appreciate all the help. Thanks!

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    I wouldn't separate these two arrays at all. $close_people is a multidimensional array of the people, all you need to do is add another field to that. Then use a usort to sort them up in a single step.
    PHP Code:
    {
        
    $close_people[$member['member_id']] = $member;
        
    $close_people[$member['member_id']]['similarity'] = similarity($_SESSION['member_id'], $member['member_id']);
    }

    function 
    sortBySimilarityASC($a$b)
    {
        
    $iResult 0;
        if (isset(
    $a['similarity'], $b['similarity']))
        {
            
    $iResult $a['similarity'] - $b['similarity'];
        }
        return 
    $iResult;
    }

    function 
    sortBySimilarityDESC($a$b// in case you want a desc
    {
        return 
    sortBySimilarityASC($b$a);
    }

    usort($close_people'sortBySimilarityASC'); 
    You can also modify the function so that if the result of $iResult is 0, and the array indexes are valid you can compare the usernames for example. That way if you have two identical similarities, it will be sorted as a secondary using that field.
    You can do this with an array_multisort as well, but I've always used delegate sorting, and honestly don't think I could write an example for a multisort without the data.

  • Users who have thanked Fou-Lu for this post:

    abelusmc (03-25-2012)

  • #3
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thank you very much, sorting to me at this point seems tricky! (I was trying to visualize what an array would be if it were a real world object, like a box with stuff in it) I am going to add that code *** is and see how it turns out. Thanks again


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •