mindspin311 Posted February 7, 2008 Share Posted February 7, 2008 Hi. I'm trying to find a sort function that will sort by the value without changing the keys. Is their one, or do I have to make this? I read that array_multisort doesn't change the key if it's a string, but I use a 9 digit numerical string which just gets converted to a number. Quote Link to comment https://forums.phpfreaks.com/topic/89842-solved-sort-without-changing-keys/ Share on other sites More sharing options...
teng84 Posted February 7, 2008 Share Posted February 7, 2008 instead of using the value why not use the key.. <?php $fruits = array("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple"); ksort($fruits); foreach ($fruits as $key => $val) { echo "$key = $val\n"; } ?> this works the same ! Quote Link to comment https://forums.phpfreaks.com/topic/89842-solved-sort-without-changing-keys/#findComment-460410 Share on other sites More sharing options...
mindspin311 Posted February 7, 2008 Author Share Posted February 7, 2008 instead of using the value why not use the key.. <?php $fruits = array("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple"); ksort($fruits); foreach ($fruits as $key => $val) { echo "$key = $val\n"; } ?> this works the same ! Yes.. I'm familiar with ksort. I'd rather find something that sorts values without changing the keys as I stated in my question. Since the values are not unique, I can't just switch the key and value. And I'd prefer not to change the id from '901234567' to something like 'rp1234567' Quote Link to comment https://forums.phpfreaks.com/topic/89842-solved-sort-without-changing-keys/#findComment-460417 Share on other sites More sharing options...
teng84 Posted February 7, 2008 Share Posted February 7, 2008 can you give some example that you want to do.. i dont know why you cant use key. Quote Link to comment https://forums.phpfreaks.com/topic/89842-solved-sort-without-changing-keys/#findComment-460425 Share on other sites More sharing options...
mindspin311 Posted February 7, 2008 Author Share Posted February 7, 2008 I'm making a roommate matching website. Each person as an 'id' like: '901234567' ((all numerical string)) I have an array of people with their possible matches and scores. $roommates[$id] = $score; //for each person $person[$id] = $roommates; so this is how it would look for person['123456789'] foreach ($person['123456789'] as $key => $value) { echo "$key: $value <br>"; } This would display 987654321: 77 234567890: 66 123456780: 86 ... ... the score's i store are 51-100 so their will be plenty of duplicates as I can't use the score as a key. When I sort, I lose the $key which I need to know the id of the person, so what I need is a sort function that will not re-index from 0-n, but rather keep my key as the id. Quote Link to comment https://forums.phpfreaks.com/topic/89842-solved-sort-without-changing-keys/#findComment-460433 Share on other sites More sharing options...
laffin Posted February 7, 2008 Share Posted February 7, 2008 how are u sorting? Quote Link to comment https://forums.phpfreaks.com/topic/89842-solved-sort-without-changing-keys/#findComment-460447 Share on other sites More sharing options...
mindspin311 Posted February 7, 2008 Author Share Posted February 7, 2008 Are you asking if I'm sorting by the value ($score) which I've said several times? Or are you asking if I want to sort in ascending or descending order? Not sure how that matters... One last time.... Sorting by the value (which has duplicates) Don't want to change the key (which obviously doesn't have duplicates) Can't use array_flip function since the value i'm sorting by has duplicates. Don't feel like creating my own sort that does this. I just was wondering if their was a function like no_change_keys_sort($array, ASC | DESC | ...) Quote Link to comment https://forums.phpfreaks.com/topic/89842-solved-sort-without-changing-keys/#findComment-460457 Share on other sites More sharing options...
roopurt18 Posted February 7, 2008 Share Posted February 7, 2008 I don't understand why you can't use the score as the key. In instances where you have a collision (i.e. duplicate score) just turn the value into an array. Quote Link to comment https://forums.phpfreaks.com/topic/89842-solved-sort-without-changing-keys/#findComment-460458 Share on other sites More sharing options...
teng84 Posted February 7, 2008 Share Posted February 7, 2008 $fruits = array(5=>"lemon", 4=>"orange", 2=>"banana", 6=>"apple"); $fruits2 = array(5=>"lemon", 4=>"orange", 2=>"banana", 6=>"apple"); sort($fruits); $x= 0; $z= array(); foreach ($fruits as $val){ $new = array($val); $result = array_intersect($fruits2, $new); print_r($result);echo '<br>'; } this? output Array ( [6] => apple ) Array ( [2] => banana ) Array ( [5] => lemon ) Array ( [4] => orange ) Quote Link to comment https://forums.phpfreaks.com/topic/89842-solved-sort-without-changing-keys/#findComment-460464 Share on other sites More sharing options...
mindspin311 Posted February 7, 2008 Author Share Posted February 7, 2008 Well that works if their are no duplicate results. For that, just doing array_flip before and after a ksort would be easier. If their are duplicates, I get something like: Array ( [4] => apple [6] => apple ) Array ( [4] => apple [6] => apple ) Array ( [5] => lemon ) Array ( [2] => pear ) I want: 4 => apple 6 => apple 5 => lemon 2 => pear I really don't want to do nested arrays. Quote Link to comment https://forums.phpfreaks.com/topic/89842-solved-sort-without-changing-keys/#findComment-460471 Share on other sites More sharing options...
roopurt18 Posted February 7, 2008 Share Posted February 7, 2008 Where exactly are you getting this data? And what formula are you using to calculate the score? If you're using MySQL you might be able to just shove everything over to the DB engine and have it sort the results for you. Quote Link to comment https://forums.phpfreaks.com/topic/89842-solved-sort-without-changing-keys/#findComment-460474 Share on other sites More sharing options...
teng84 Posted February 7, 2008 Share Posted February 7, 2008 Where exactly are you getting this data? And what formula are you using to calculate the score? If you're using MySQL you might be able to just shove everything over to the DB engine and have it sort the results for you. if your not using SQL you may use SJF algorithm or shortest job first.. i believe you can compare string in php like a<b is true.. Quote Link to comment https://forums.phpfreaks.com/topic/89842-solved-sort-without-changing-keys/#findComment-460479 Share on other sites More sharing options...
mindspin311 Posted February 7, 2008 Author Share Posted February 7, 2008 Where exactly are you getting this data? And what formula are you using to calculate the score? If you're using MySQL you might be able to just shove everything over to the DB engine and have it sort the results for you. I'm calculating all the data. I take a quesionaire from 2 people and generate a score based on similar matches. None of the data below is in the database. I just store the person id and their answers in the database. One person will have an array like this... $person = Array($id => $roommates) where $id is the person's id and $roommates is an array of possible roommates like so: $roommate = Array($id => $score) where $id is the roommate's id and $score is the compatibility of the 2 people. So for person [100] we might have 101: 66 102: 77 103: 88 104: 77 105: 99 106: 66 I want to sort in descending order and get: 105: 99 103: 88 102: 77 104: 77 101: 66 106: 66 (where for the elements 102/104 and 101/106 the order doesn't really matter.. 104 could come before 102, but the way I have it above is prefered) Quote Link to comment https://forums.phpfreaks.com/topic/89842-solved-sort-without-changing-keys/#findComment-460481 Share on other sites More sharing options...
rhodesa Posted February 7, 2008 Share Posted February 7, 2008 i don't know if this is the easiest, but here is one way: code: <?php $list = array( '987654321' => 77, '234567890' => 66, '123456780' => 86, ); $id_list = array_keys($list); $score_list = array_values($list); array_multisort($score_list,$id_list); $new_list = array_combine($id_list,$score_list); print_r($new_list); ?> output: Array ( [234567890] => 66 [987654321] => 77 [123456780] => 86 ) Quote Link to comment https://forums.phpfreaks.com/topic/89842-solved-sort-without-changing-keys/#findComment-460487 Share on other sites More sharing options...
rhodesa Posted February 7, 2008 Share Posted February 7, 2008 Use array_multisort($score_list, SORT_NUMERIC, SORT_DESC , $id_list); for descending order Quote Link to comment https://forums.phpfreaks.com/topic/89842-solved-sort-without-changing-keys/#findComment-460490 Share on other sites More sharing options...
mindspin311 Posted February 7, 2008 Author Share Posted February 7, 2008 i don't know if this is the easiest, but here is one way: code: <?php $list = array( '987654321' => 77, '234567890' => 66, '123456780' => 86, ); $id_list = array_keys($list); $score_list = array_values($list); array_multisort($score_list,$id_list); $new_list = array_combine($id_list,$score_list); print_r($new_list); ?> output: Array ( [234567890] => 66 [987654321] => 77 [123456780] => 86 ) Thank you. Thank you. Thank you!! Works great. Quote Link to comment https://forums.phpfreaks.com/topic/89842-solved-sort-without-changing-keys/#findComment-460492 Share on other sites More sharing options...
mindspin311 Posted February 7, 2008 Author Share Posted February 7, 2008 How do I mark this as solved? Quote Link to comment https://forums.phpfreaks.com/topic/89842-solved-sort-without-changing-keys/#findComment-460496 Share on other sites More sharing options...
mindspin311 Posted February 7, 2008 Author Share Posted February 7, 2008 Just found these 2 functions: asort: sorts ascending without changing keys arsort: sorts descending without changing keys although arsort sorts the indexes or duplicate values in descending order rather than keeping them in their original order in the table. Quote Link to comment https://forums.phpfreaks.com/topic/89842-solved-sort-without-changing-keys/#findComment-460567 Share on other sites More sharing options...
rhodesa Posted February 7, 2008 Share Posted February 7, 2008 Well there ya go. I figured there was a function that did it, just didn't know it off hand. Quote Link to comment https://forums.phpfreaks.com/topic/89842-solved-sort-without-changing-keys/#findComment-460700 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.