0

I collect the files from a folder using some Smarty code:

{assign var=files value="uploads/Documenten/GPS-Tracks/*.html"|glob}

{if count($files)}
    <ul>
        {foreach from=$files item='file'}
            {assign var='gpstrack_location' value="{$file|cms_escape:'urlpathinfo'}"}
            {if !$file|contains:"index"}
                <li><a href="{$file}">{$file|basename|substr:0:-5}</a></li>
            {/if}
        {/foreach}
    </ul>
{/if}

This gives me a sorted list basedon the first character.

  • Brighton-Gosport_2108
  • Brighton-Littlehampton_2012
  • Brighton-Portmouth-Brighton_2018
  • Bruinisse-Herkingen_2012
  • BurnhamonCrouch_Vlissingen_22aug13
  • Calais-Dieppe_2014
  • Carteret-Dielette_2012
  • Cherbourg-StVaast_2012
  • Clubwedstrijd_17sept11
  • Clubwedstrijd_18sep10
  • Clubwedstrijd_21sep13
  • Cowes-BeauLieu_River_2012
  • Dartmouth-Guernsey_2012
  • Deauville-Honfleur_2012
  • Denemarken_2008
  • Dielette-Alderney_2012
  • Dieppe-Boulogne_2012

What I'd like to achive is a sorted list based on the last two charaters. Is there in Smarty or PHP a way to do this?

Thanks in advance for your help!

UPDATE: I got myself a couple of steps futher :) There remains one warning I'm not able to solve: The code so far:

<?php

    $folder = '<some foldername>';

    $files = array_values(preg_grep('/^([^.])/', scandir($folder, SCANDIR_SORT_ASCENDING)));
    //$files = scandir($folder);

    //print_r($files);

    usort(var_dump($files, function ($a, $b){
        if ($a || $b <> "")
            return substr($b, strlen($b) - 2) - substr($a, strlen($a) - 2);
    }));
?>

The warning I get, and thought to solve with the 'or'-statement:

[

  • 121] => string(29) "Weymouth-Lyme_Reges_2012.html"
  • [122]=> string(24) "Yarmouth-Poole_2012.html" }
  • object(Closure)#1036 (1) {["parameter"]=> array(2) { ["$a"]=> string(10) "" ["$b"]=> string(10) "" } } Warning: usort() expects exactly 2 parameters, 1 given in /Users/name/Sites/install/assets/udt/sort-on-last-characters.php on line 55

Someone able to get me a step further?

Rob
  • 26,989
  • 16
  • 82
  • 98
user2037412
  • 164
  • 1
  • 11
  • Similar: https://stackoverflow.com/questions/31306951/python-how-to-sort-list-by-last-character-of-string – Akshay Gohil Aug 17 '18 at 07:59
  • 1
    Thanks Akshay. I found that example/answer too, however this is written in Python, and I didn't manage to 'convert' the Python script to Smarty. Therefor I opened this topic. – user2037412 Aug 17 '18 at 11:06
  • @user2037412 The question's now been re-opened. Please post your answer as an answer, rather than as an edit to the question (even if it's been closed). I've rolled back that edit - you may still access it [here](https://stackoverflow.com/posts/51869836/revisions) – Rob Aug 23 '18 at 05:39

1 Answers1

0

Found a solution.

<?php
$files = glob($folder.$file_name);
$current_year = date('y');
if (
    usort($files, function($a, $b) {
        $yearA = (int) substr($a, -7); // extension + . + last two characters
        $yearB = (int) substr($b, -7);
        return $yearB - $yearA; // descending, for ascending swap $yearA and $yearB
    })
) {
    // $files is now sorted, do your stuff here
    foreach ((array) $files as $file) {

        if (substr(basename($file, '.html'), -2) == $current_year) {
            echo basename($file, '.html');
            echo "<br />";
        } else {
            $current_year = $current_year - 1;

        }
    }

} else {
    // sorting failed, show error message, exception, etc.
    echo "Files not sorted";
}    
?>
user2037412
  • 164
  • 1
  • 11