Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tablesorter - Dates Problem

I know there have been some questions about this, but I can't seem to fix my problem.

I'm loading a .csv file into tablesorter, but one of my columns are dates (Dec 23, 2009). But they sort as Dec 2, Dec 23, Dec 3, Dec 31

Does anyone know a solution? You can see the problem here, it's the table at the bottom. Thanks so much in advance!

<script type="text/javascript" charset="utf-8">
  $(document).ready(function() 
      { 
          $("#tablesorter-demo2").tablesorter({ widgets: ['zebra'] });
      } 
  );
 </script>

-

table width="871" border="0" cellpadding="0" cellspacing="1" class="tablesorter" id="tablesorter-demo">

$row = 1;
$handle = fopen("csv/canadatransactions.csv",

"r"); while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { $num = count($data); $row++;

                if ($row == 2)
               {
                   echo "<thead>\n<tr>\n";

                   echo "<th class=\"header\">" . $data[1] .

"\n"; // name echo "" . $data[0] . "\n"; // symbol echo "" . $data[2] . "\n"; // buy sell echo "" . $data[3] . "\n"; // date echo "" . $data[4] . "\n"; // shares echo "" . $data[5] . "\n"; // price echo "" . $data[6] . "\n"; // cash value

                   echo "</tr>\n</thead>\n<tbody>";
               }

               else
               {
                   echo "<tr class=\"even\"";
                   echo ">\n";
                   echo "<td>" . $data[1] . "</td>\n";
    echo "<td>" . $data[0] . "</td>\n";
    echo "<td>" . $data[2] . "</td>\n";
    echo "<td>" . $data[3] . "</td>\n";
    echo "<td>" . $data[4] . "</td>\n";
    echo "<td>C$ " . $data[5] . "</td>\n";
    echo "<td>C$ " . $data[6] . "</td>\n";

                   $transactions = $row - 3;
 }
}
fclose($handle);
?>
            </tbody>
</table>
like image 332
Mike K. Avatar asked Dec 24 '09 14:12

Mike K.


2 Answers

It's sorting based on alphanumeric (ASCII) values, not based on date-values (It will also put November before October). You need jquery to interpret each cell as a date value and then sort them accordingly. If their built-in type detection isn't working, you have to force the data type. See http://www.terminally-incoherent.com/blog/2008/09/29/jquery-tablesorter-list-of-builtin-parserssorters/ for more information.

like image 83
Jason Yanowitz Avatar answered Nov 13 '22 23:11

Jason Yanowitz


You should specify the type of the column. Otherwise it will be sorted as text. You can do it by specyfing sorter parameter:

<table>
<thead>
    <tr>
        <th>Id</th>
        ... other columns ....
        <th class="{sorter: 'isoDate'}">Date</th>
</tr>           
</thead>
<tbody>
... table body ....

Alhough I'm not sure if isoDate is the sorter you should use, but tablesorter have two other sorters: usLongDate and shortDate. You can try which one of those will do the job.

like image 1
Lukasz Lysik Avatar answered Nov 13 '22 23:11

Lukasz Lysik