Jump to content

[SOLVED] sort without changing keys


mindspin311

Recommended Posts

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 !

Link to comment
Share on other sites

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'

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 | ...)

 

Link to comment
Share on other sites

$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 )

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.. 

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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
)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.