Jump to content

Help with sorting


Go to solution Solved by Barand,

Recommended Posts

I am creating an automatic English Resume writing program for non-Native English speakers and all has went well so far, but some people simply don't understand that the resume should be reverse chronological so i need to sort the jobs by year.

 

Each job looks something like this

 

$JOBDISPLAY10a= "<div><div class='colleft'>". $Jobtitle10a.", ". $Company10a.", ". $Jobcountry10a.
"</div><div class='colright'>". $JOBSTART10a." - ".$JOBFINISH10a."</div></div>".
$jobdutyfull10a. "<br>";

 

I then

echo $JOBDISPLAY10a

echo $JOBDISPLAY11a

ect.

I know that this is pretty good foundation to sort the jobs but I am unsure of how to sort the echo $JOBDISPLAYX variables by the $JOBFINISHX variables, especially since $JOBFINISHX could = Present. What makes this even tougher is that multiple jobs may be listed as "present" Meaning that I would have to order them, and only them, with $JOBSTARTX.

I've tried various approaches but I cannot get it to function in a reverse chronological sense with Present at the top. Any ideas?

Thanks

-Eric

Link to comment
Share on other sites

  • Solution

If you have the data in a database, sort it when you retrieve the date using an ORDER BY clause.

 

Otherwise, don't use variables like $JOBDISPLAY10, $JOBDISPLAY11, use an array and sort the array.

 

EG

<?php
$jobdisplay = [
             10 => [ 'title'   => 'Job title 10',
                     'company' => 'Company name 10',
                     'country' => 'Somewhere',
                     'start'   => '2016-05-01',
                     'finish'  => 'present',
                     'duties'  => 'description of duties ...'
                     ],
             11 => [ 'title'   => 'Job title 11',
                     'company' => 'Company name 11',
                     'country' => 'Somewhere else',
                     'start'   => '2014-01-01',
                     'finish'  => '2014-12-31',
                     'duties'  => 'another description of duties ...'
                     ],
             12 => [ 'title'   => 'Job title 12',
                     'company' => 'Company name 12',
                     'country' => 'Somewhere',
                     'start'   => '2016-04-01',
                     'finish'  => 'present',
                     'duties'  => 'yet another description of duties ...'
                     ],
             13 => [ 'title'   => 'Job title 13',
                     'company' => 'Company name 13',
                     'country' => 'Somewhere other',
                     'start'   => '2015-01-01',
                     'finish'  => '2016-03-31',
                     'duties'  => 'and yet another description of duties ...'
                     ]
        ];
        
function customSort($a, $b)
{
    $finA = $a['finish']=='present' ? date('Y-m-d') : $a['finish'];
    $finB = $b['finish']=='present' ? date('Y-m-d') : $b['finish'];
    
    $x = strcmp($finB, $finA);
    // if same finish dates, sort by start date
    if ($x==0) {
        return strcmp($b['start'], $a['start']);
    }
    return $x;
}

usort($jobdisplay, 'customSort');

echo '<pre>';
foreach ($jobdisplay as $j) {
    printf('%-20s | %-15s | %-15s<br>', $j['title'], $j['start'], $j['finish']);
}
echo '</pre>';
?

Gives

Job title 10         | 2016-05-01      | present        
Job title 12         | 2016-04-01      | present        
Job title 13         | 2015-01-01      | 2016-03-31     
Job title 11         | 2014-01-01      | 2014-12-31    
  • Like 1
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.