-3

Hi I have an array like

$test = ['orange 2016','orange 2017' ,'Mango 2018' ,'apple 2018' ,'apple 2015'];

I have to sort the array so that the array should be descending order with respect to the year.I tried with different type of sorting.But all fails.My expected result is something like below

$test = ['apple 2018','Mango 2018','Orange 2017','Orange 2016' ,'apple 2015'];

My code is shared below

  $test = ['jan 2016','jan 2017' ,'Dec 2018' ,'April 2018' ,'March 2015'];

echo "<pre>";
print_r($test);
echo "</pre>";

$n = count($test);
for($i=0;$i<$n;$i++){
    for($j=$i+1;$j<($n);$j++){
         preg_match('#(\d+)$#',$test[$i],$year_one);
         preg_match('#(\d+)$#',$test[$j],$year_two);
         if($year_one[1] < $year_two[1]){
            $temp = $test[$j];
            $test[$j] = $test[$i];
            $test[$i] = $temp;
         }
         if($year_one[1] == $year_two[1]){
            if(strcmp($test[$j],$test[$i]) < 0 ){
                $temp = $test[$j];
                $test[$j] = $test[$i];
                $test[$i] = $temp;
            }
         }
    }
}

echo "<pre>";
print_r($test);
echo "</pre>";

It is little complicated code.Is any other simplest method for achieving the desired result?

2
  • What do you have so far and how is it not working? Commented Aug 18, 2017 at 6:59
  • I edited my question Commented Aug 18, 2017 at 7:07

1 Answer 1

1

So I explode the words in to two new arrays and sort them with multisort and year as the leading array.
Then I rebuild the new result array.

$test = ['Red orange 2016','orange 2017' ,'Mango 2018' ,'Granny Smith apple 2018' ,'apple 2015'];

$a = array(); // create two new arrays to hold fruit and year
$b = array();
$temp = array();
foreach($test as $item){
    $temp = explode(" ", $item); // explode the fruit/year to temp array
    $a[] = implode(" ", array_splice($temp, 0, -1)); // implode all but the last item as "fruit"
    $b[] = end($temp); // last item is the year
}

array_multisort($b, $a); // sort the new arrays

$result=array();
for($i=count($b)-1; $i>=0; $i--){ // looping backwards to only count() once. (faster)
    $result[] = $a[$i] . " " . $b[$i]; // rebuild the new array with correct sorting.
}
var_dump($result);

https://3v4l.org/eCi7J

EDIT; I use a temp array to hold the exploded values and use array_splice and implode to build fruits, and separate the year.

Sign up to request clarification or add additional context in comments.

5 Comments

Your answer is nice.Its works well for this array.But the element may contain elements with more than one space.
@AswathyS Try it now.
how can it is implemented through sql query? Is it possible?
I don't know. Your question is very vauge. Start a new topic instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.