1

I have a program where I receive a string at the input and store it line by line in the field, for example list [0] = 'Roll 5CZK', etc .. I also have a function that finds the value from the string (number, here 5). How can I sort this field. I don't want to solve it with a 2D array

$string='Rohlík 5Kč
CZK400 Knížka
Pivo 42,-
Houska 4 Kč';
$list = explode(PHP_EOL, $string);

getPrice($list[1]); // => 400
getPrice($list[2]; // => 42
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
  • 1
    There's a [list of sort functions](https://www.php.net/manual/en/array.sorting.php). Pick the one you need. Without knowing what `getPrice()` is doing I can't tell which one fits best. There is nothing wrong with creating a helper array to sort it out. – Markus Zeller Oct 13 '21 at 12:00
  • Sure, I can create an auxiliary field and then compare, but I wonder if it's easier. The function (my written one) returns the numerical value from the string, I tried to indicate it in the code. –  Oct 13 '21 at 12:07
  • Something like this `usort($list, fn ($a, $b) => getPrice($a) <=> getPrice($b));`? Though note that, `getPrice` might be called repeatedly for the same element. If that is a problem you should map to some intermediary data structure, sort and map back afterwards. – Yoshi Oct 13 '21 at 12:13
  • 2
    But I can not understand the problem. What exactly do you want to sort? Can you please refine your question and add the expected result? – Markus Zeller Oct 13 '21 at 12:34
  • Does this answer your question? [How can I sort arrays and data in PHP?](https://stackoverflow.com/questions/17364127/how-can-i-sort-arrays-and-data-in-php) – CBroe Oct 13 '21 at 13:36

2 Answers2

1

Solution with usort as in the comment, only $a and $b swapped to achieve a descending sort.

$arr = array (
  0 => "Rohlík 5Kč",
  1 => "CZK400 Knížka",
  2 => "Pivo 42,-",
  3 => "Houska 4 Kč",
);

/*
 * This is a greatly simplified implementation of getPrice()
 * to make the code reproducible
 */
function getPrice($str){
  return preg_replace('~[^\d]~u','',$str);
}

//Sorting
usort($arr,function($a,$b){
  return getPrice($b) <=> getPrice($a);
});

// Test Output
var_export($arr);

Output:

array (
  0 => 'CZK400 Knížka',
  1 => 'Pivo 42,-',
  2 => 'Rohlík 5Kč',
  3 => 'Houska 4 Kč',
) 
jspit
  • 7,276
  • 1
  • 9
  • 17
-1

foreach ($list as $v) $arr1[$v]=getPrice($v); rsort($arr1);

... and you are done!

Adrian Goia
  • 151
  • 3
  • 7
  • [A code-only answer is not high quality](//meta.stackoverflow.com/questions/392712/explaining-entirely-code-based-answers). While this code may be useful, you can improve it by saying why it works, how it works, when it should be used, and what its limitations are. Please [edit] your answer to include explanation and link to relevant documentation. – Stephen Ostermiller Oct 13 '21 at 18:11
  • @Ostermiller: of course, old fellow. Why it works? I don't know. Perhaps it's a question of 1 and 0. How it works? See above. – Adrian Goia Oct 13 '21 at 19:37
  • Rather than adding details in comments, please [edit] your answer to add requested information. You can then comment saying that you have done so. Comments here may get hidden by "show more comments" or could be deleted without notice. Editing updates into your answer ensures that they don't get lost and that everybody who wants to use your answer sees them. – Stephen Ostermiller Oct 13 '21 at 20:11