KoolReport's Forum

Official Support Area, Q&As, Discussions, Suggestions and Bug reports.
Forum's Guidelines

Custom Sort #260

Open arthur opened this topic on on Apr 26, 2018 - 4 comments

arthur commented on Apr 26, 2018

Hi,

I got a column with string dates (January, February etc...). Problem when I want to sort this dates, it's not a chronological but alphabetical order. How could I do to have first january, then february etc.

Here is my code

->pipe(new Group(array(
            "by"=>"Date",
        )))

->pipe(new Sort(array(
            "Date"=>"asc"
        )))
        ->pipe($this->dataStore('date'));

Thanks a lot

David Winterburn commented on Apr 26, 2018

Hi Arthur,

Beside "asc" and "desc" directions, the Sort process could use a custom function as well. For your case please use the following code:

->pipe(new Sort(array(
            "Date"=>function($m1, $m2) {
                $map = array(
                    'January' => 1,
                    'February' => 2,
                    ...
                    'December' => 12
                );
                return $map[$m1] < $map[$m2];
            }
        )))

Hope this helps. Thanks!

arthur commented on Apr 26, 2018

It works thanks ! I'm just wondering, I got a "TOTAL" appearing but doesnt exist in the column. Do you know why ?

David Winterburn commented on Apr 26, 2018

Hi Arthur,

Can you please check the date column and see if there's any value not of month names? Thanks!

arthur commented on Apr 26, 2018

It's fine I find a way with Map. Thanks !

Build Your Excellent Data Report

Let KoolReport help you to make great reports. It's free & open-source released under MIT license.

Download KoolReport View demo
help needed
solved

None