0

I have an array like this:

$numbers = [20, 2, 248, 992, 42]

I want to calculate the difference between the highest and lowest numeral. For instance of 248 it should be 8 - 2 = 6 (highest is 8, lowest is 2). Another example: 992 = 9 -2 = 7 (again, highest - lowest).

As a result, I want to output the input-array in the correct order, according to the difference in the math described above.

To give a complete example:

Input-Array: $numbers = [20, 2, 248, 992, 42]

Math:

20  = 2 - 0 = 2 *
2   = 2 - 2 = 0
248 = 8 - 2 = 6
992 = 9 - 2 = 7
42  = 4 - 2 = 2 *

Output-Array: 2, 42, 20, 248, 992

(Since the 42 is behind the 20 in the input-array, this number should come first in the sorting.

I tried the following so far:

function digitDifferenceSort($a) {
    $out = array();

    foreach($a as $z) {
        $number = str_split($z);    
        sort($number);

        // substract the biggest minus the smallest
        $diff = $number[max(array_keys($number))] - $number[0];
        $out[] = $diff;
    }

    // format again
    asort($out);


    // iterate through the reordered out-array and get the values
    $reorderedArray = array();

    foreach($out as $k => $reordered) {   
        $reorderedArray[] = $a[$k];
    }
}

This works "so far, so good", but is struggling with the problem I described above, if the same difference comes again. Maybe asort() isn't the right thing to do here? Maybe usort would be the right approach?

DasSaffe
  • 2,080
  • 1
  • 28
  • 67
  • Hint: `$n = str_split($a); max($n) - min($n)`. Define a simple `usort` comparison function with that. – deceze Mar 11 '19 at 10:32
  • @deceze I don't know if that "closing" was justified. From my point of view, I cannot define a `usort` function without having access to the keys. Also, this is indeed a one-dimensional array, but yet "complex", since it has to combine two sorting-algorythms simultaniously. – DasSaffe Mar 11 '19 at 10:39
  • Well, here's an example of how to use an array's keys *and* values in a `usort`: https://stackoverflow.com/a/32505181/476 – deceze Mar 11 '19 at 10:44
  • You are the boss, I guess. Had a look at it, but I still think this is something different, since I simply cannot compare 2 values with each other, since they can be far apart of each other. Having another `foreach` in the end (e.g.) would bring down the performance by too much, I guess. Wouldn't have asked If I wouldn't need help, but I guess I'll try it with your linked solutions. Thanks. – DasSaffe Mar 11 '19 at 10:49
  • I don't see how you *cannot* implement the algorithm using `usort`. Each value goes through some calculation (your min-max-subtraction) which results in the value that you want to sort by. So basically `usort($arr, function ($a, $b) { return minMaxSub($a) - minMaxSub($b); }` should do just fine. If you need the array's keys for some reason too, see the other example and adjust your sort accordingly. If it does not apply, clarify your question why it doesn't. – deceze Mar 11 '19 at 10:53

0 Answers0