Sorting example for Skrible

Nimien mukaan, Tunnisteiden mukaan, Koon mukaan

4.0 kt
.12.0 kt
dvbsnoop-MUX3-pid1639.2 kt
dvbtextsubs-0.2.targz31.1 kt
Eeee-selvaeml1.7 kt
epgsearch-errortxt5.2 kt
fipo6.2 kt
filestreamphp~0.8 kt
glibc-gij-apparent-memleak-patchdiff1.9 kt
graphedit011008zip430.5 kt
indexphp1.5 kt
indexphp~1.5 kt
keymap10.0 kt
kone-lspci1.6 kt
kuunteluportittxt4.7 kt

Sivun koodi:

<html>
<head>
<title>Skriblesort</title>
</head>

<body>

<h2>Sorting example for Skrible</h2>

<p><a href="?sort=nimi">Nimien mukaan</a>, <a href="?sort=ext">Tunnisteiden mukaan</a>, <a href="?sort=koko">Koon mukaan</a></p>

<table>
<?php

$sortinki = "nimi";

if (in_array($_GET["sort"], array("nimi", "ext", "koko")))
    $sortinki = $_GET["sort"];


function omacomp($a, $b) {
    global $sortinki;
    switch ($sortinki) {
        default:
        case "nimi":
            $res = strnatcasecmp($a['nimi'], $b['nimi']);
            break;
        case "ext":
            $res = strnatcasecmp($a['ext'], $b['ext']);
            break;
        case "koko":
            $res = $a['koko'] < $b['koko'] ? -1 : ($a['koko'] > $b['koko'] ? 1 : 0);
            break;
    }
    if ($res)
        return $res;
    $res = ($sortinki != "nimi") ? strnatcasecmp($a['nimi'], $b['nimi']) : 0;
    if ($res)
        return $res;
    return strnatcasecmp($a['ext'], $b['ext']);
}    

function split_name($vidfile) {
    $loc = strrpos($vidfile, ".");
    if ($loc === false)
        return array($vidfile, "");
    return array(substr($vidfile, 0, $loc), substr($vidfile, $loc+1));
}

$dvid = opendir(".");
$files = array();
while(($vidfile = readdir($dvid)) !== false) {
    list($nimi, $ext) = split_name($vidfile);
    $files[] = array("nimi" => $nimi, "koko" => filesize($vidfile), "ext" => $ext);
}

usort($files, "omacomp");

foreach($files as $file) {
    echo '<tr><td>'.$file['nimi'].'</td><td>'.$file['ext'].'</td><td>'.sprintf('%01.1f',$file['koko']/1024).' kt</td></tr>';
}


?>
</table>
<p><strong>Sivun koodi:</strong></p>
<?php
highlight_file(__FILE__);
?>
</body>
</html>