KoolReport's Forum

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

Sort Method doesn't work #1663

Open Thomas opened this topic on on Oct 14, 2020 - 6 comments

Thomas commented on Oct 14, 2020

(https://www.koolreport.com/docs/datastore/overview/#sort-methods-sort)

I am trying to sort my DataStore but it won't work. I've tried the following:

$groups_data_main_survey_object->sort(array('group_0_difference_avg'=>'asc'))
$groups_data_main_survey_object->sort('group_0_difference_avg','asc')
$groups_data_main_survey_object->sortBy('group_0_difference_avg','asc')
KoolReport commented on Oct 14, 2020

That's strange. We have tested and it seems fine. Could you please give us more details: the original data, what expected result and the outcome.

Thomas commented on Oct 14, 2020

Sure. I converted a multi-dimensional array into a DataStore. Here is the code and the datatable.

    $groups_data_main_survey_koolreport_object = new \koolreport\core\DataStore($groups_data_main_survey);
    $survey_codes_object = new \koolreport\core\DataStore(reset($survey_codes));
    $groups_data_main_survey_object = $survey_codes_object->join($groups_data_main_survey_koolreport_object,array("kfza_code"=>"title")) ;

Thomas commented on Oct 14, 2020

I think this is related: https://www.koolreport.com/forum/topics/1656

To use the charts option, I had to transform the object to an array.

Thomas commented on Oct 16, 2020

That's a big problem for me. All my arrays aren't accepted by the charts. I have to transform every array into a koolreport object via "new \koolreport\core\DataStore($array);", than I have to sort or filter it and than turn it back into an array via toArray(), otherwise it doesn't work. I don't want to do that for every array I have to be able use it. Because it didn't show up or the labels aren't displayed correctly.

Why can't I use a simple array? Is there an other way to transform the array into a koolreport object?

Do I have to be aware of by creating the array?

Here is the latest array, which I had to transform to be able to use:

Thomas commented on Nov 3, 2020

After additional testing, I've noticed, that koolreport have a new sorting. After rounding the values for debuging, I realized, that koolreport is sorting the value as "Int". Is it suppose to work that way?

How can is it possible to sort double/floating value with koolreport?

Sebastian Morales commented on Nov 4, 2020

Thomas, here's the sort function by DataStore:

        usort($this->rows, 
            function ($a, $b) use ($sorts) {
                $cmp = 0;
                foreach ($sorts as $sort => $direction) {
                    if (is_string($direction)) {
                        $cmp = is_numeric($a[$sort]) && is_numeric($b[$sort]) ? 
                            $a[$sort] - $b[$sort] : strcmp($a[$sort], $b[$sort]);
                        $cmp = $direction === 'asc' ? $cmp : - $cmp;
                    } else if (is_callable($direction)) {
                        $cmp = $direction($a[$sort], $b[$sort]);
                    }
                    if ($cmp !== 0) {
                        break;
                    }
                }
                return $cmp;
            }
        );

If your values are all numbers or numeric strings (int, float, double, etc) a simple arithmetic comparison is used. Otherwise, string comparison is used. You also have another option to use an anonymous function for comparison like this:

$myDS->sort(array("column1" => function($a, $b) {
    //compare $a and $b here and return result.
}));

In case there's still confusion please post one of your sample data here (or via email) together with your sorting code. We will test it directly for you. Cheers, Sebastian.

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
bug

None