kyleabaker Posted April 19, 2007 Share Posted April 19, 2007 Hey, I'm trying to make my own search page for my website and I have it gathering correct results exactly as it should, but I'd like to find a way to have it sort these results according to their relevance (makes mroe sense of course). As it is now, it is simply pulling them from my database newest-to-oldest. The array elements are compiled like this.. type_of_item.id_#_of_item.hits_for_item.data ..so you can see that I have them all seperated by periods, then to print the data I just get the length that it is from the left and echo it out. I have all of that working, but I need to find a way to sort my array by the 3rd period seperated part...the hits. I want the items with the most hits at the top. Anyone have any ideas or help? Thanks in advanced. Link to comment https://forums.phpfreaks.com/topic/47677-need-help-sorting-an-array/ Share on other sites More sharing options...
Moon-Man.net Posted April 19, 2007 Share Posted April 19, 2007 Can you please post some code that you are using to extract and such? What DBMS are you using? -- Nathan Link to comment https://forums.phpfreaks.com/topic/47677-need-help-sorting-an-array/#findComment-232819 Share on other sites More sharing options...
Glyde Posted April 19, 2007 Share Posted April 19, 2007 <?php function sortByHits($l, $r) { return $l[2] == $r[2] ? 0 : ($l[2] < $r[2] ? -1 : 1); } usort($resultArray, "sortByHits"); ?> That should work, but it's untested. Link to comment https://forums.phpfreaks.com/topic/47677-need-help-sorting-an-array/#findComment-232820 Share on other sites More sharing options...
kyleabaker Posted April 19, 2007 Author Share Posted April 19, 2007 Moon-Man.net I'm using MySQL database. I'll post some code in a bit, gonna try what Glyde posted first. Glyde Thanks, gonna test it, I'll be back. Link to comment https://forums.phpfreaks.com/topic/47677-need-help-sorting-an-array/#findComment-232836 Share on other sites More sharing options...
kyleabaker Posted April 19, 2007 Author Share Posted April 19, 2007 Glyde I tested that and changed $resultArray to the array that contains the search results ($searchresults) and here is what it says: Warning: usort() [function.usort]: The argument should be an array in /***********/blah.com/search.php on line 60 Link to comment https://forums.phpfreaks.com/topic/47677-need-help-sorting-an-array/#findComment-232842 Share on other sites More sharing options...
kyleabaker Posted April 19, 2007 Author Share Posted April 19, 2007 Sorry, i had a small typo in the code, so it is sorting now. It's still not sorting completely correctly, but I'm gonna do some debugging and get back to you all. Link to comment https://forums.phpfreaks.com/topic/47677-need-help-sorting-an-array/#findComment-232855 Share on other sites More sharing options...
kyleabaker Posted April 19, 2007 Author Share Posted April 19, 2007 What exactly is this line doing... return $l[2] == $r[2] ? 0 : ($l[2] < $r[2] ? -1 : 1); can anyone explain it? Cause I had my search echo out the number of hits for each item and I'm seeing the following list.. 1 1 3 1 1 1 ..and it should be.. 3 1 1 1 1 1 ...maybe it is comparing in the wrong place..? Not sure tho, I just don't understand what that line is checking exactly. Link to comment https://forums.phpfreaks.com/topic/47677-need-help-sorting-an-array/#findComment-232856 Share on other sites More sharing options...
kyleabaker Posted April 19, 2007 Author Share Posted April 19, 2007 So it seems to be sorting the array based off of the 3rd character in the list. ie. news.id.hits.data screenshots.id.hits.data sorts to.. screenshots.id.hits.data news.id.hits.data ..i'm trying to figure out how to sort it by the value of hits, so i need to split by chr(46) and compare values. so i finally fixed it by using this.. <?PHP function sortByHits($l, $r) { $l_exploded = explode(chr(46),$l); $r_exploded = explode(chr(46),$r); return $l_exploded[2] == $r_exploded[2] ? 0 : ($l_exploded[2] > $r_exploded[2] ? -1 : 1); } usort($searchresults, "sortByHits"); ?> thanks so much Glyde for the help! Don't laugh at my terrible variable names, hehe. Link to comment https://forums.phpfreaks.com/topic/47677-need-help-sorting-an-array/#findComment-232870 Share on other sites More sharing options...
Glyde Posted April 19, 2007 Share Posted April 19, 2007 Ahh, I figured that you already had it split by the period, my fault . I had assumed you had them into proper arrays already and just needed the sort function. Anyways, I'm glad I could help. Link to comment https://forums.phpfreaks.com/topic/47677-need-help-sorting-an-array/#findComment-233163 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.