0

I have a function that looks like this

public function dod()
{
    $this->load->model('deals');
    $unsortedData['deals'] = $this->deals_of_day->get_todays_deals();
    $data['deals']=$this->sort_deals_by_expiration_time($unsortedData['deals']);

    $this->load_view('admin/dod', $data);
}

Where, $unsortedData['deals'] looks like this:

array (size=3)
0 =>
object(stdClass)[64]

public 'created_at' => string '1515477074' (length=10)
public 'updated_at' => string '1515477074' (length=10)
public 'expirationTime' => string '1515479400' (length=10)
1 =>
object(stdClass)[65]
public 'created_at' => string '1515477075' (length=10)
public 'updated_at' => string '1515477075' (length=10)
public 'expirationTime' => string '1515479400' (length=10)
2 =>
object(stdClass)[66]

public 'created_at' => string '1515477075' (length=10)
public 'updated_at' => string '1515477075' (length=10)
public 'expirationTime' => string '1515479400' (length=10) 

I wish $data['deals'] to be a sorted array by expirationTime in ascending order.

For reference I saw this answer for sorting an array of objects , and also I saw this answer but they dont seem to fit my case.

Help me with this. Thanks.

Sarthak Batra
  • 487
  • 2
  • 10
  • 24
  • I assume data comes from a database. Why don't you just sort it from your query? – Miggy Jan 09 '18 at 07:18
  • Yes, it does come from MySQL database. I come from MEAN stack and am new to LAMP stack. Can you please tell me how to sort it from query? @Miggy – Sarthak Batra Jan 09 '18 at 07:20

3 Answers3

3

Replace $data['deals']=$this->sort_deals_by_expiration_time($unsortedData['deals']); with the following code.

// Asc sort

    usort($unsortedData,function($first,$second){
        return $first->expirationTime> $second->expirationTime;
    });

If you want to sort descending order use:

// Desc sort

    usort($unsortedData,function($first,$second){
        return $first->expirationTime< $second->expirationTime;
    }); 
Abdus Sattar Bhuiyan
  • 3,016
  • 4
  • 38
  • 72
1

You could use this.

Instead of sorting your data in php, you can do it in your query. In order to do that, you could use this code.

assuming that your function is get_todays_deals().

1st add parameter to the function. Something like this:

public function get_todays_deals($sortByField = '',$sortType = ''){
    if($sortByField == 'expirationTime' && $sortType == 'ASC' ) {
        # then do code query for sorting
        # SELECT * FROM table_name ORDER BY $sortByField $sortType; 
    }
}

making the parameter equaling to '' so that you can still use your function without parameter.

Miggy
  • 816
  • 7
  • 16
0

This is a rather simple one to do. All you have to do is a double iteration like so.

function sortUnsordedData($array) {
    $count = count($array);
    $placeholder = array();
    for ( $i1=0; $i1<$count; $i1++ ) {
        for ( $i2=0; $i2<$count; $i2++ ) {
            if ( $i1 == $i2 ) continue;
            if ( $array[$i1]['expirationTime'] < $array[$i2]['expirationTime'] ) {
                $placeholder = $array[$i1];
                $array[$i1] = $array[$i2];
                $array[$i2] = $placeholder;
            }
        }
    }
    return $array;
}

Here, we should receive an array sorted in ascending order by the expiration time. Probably not the most efficient way, but it's rather easy to understand how it works.

Michael Thompson
  • 541
  • 4
  • 21