I have a table view with exposed filters and a sort on a date field.

I added a data export with the same structure of the page, with parent sort selected, but while the exposed filters work, the sort doesn't, both with or without the exposed option selected.

I tried to debug the code, but I found no clue.

Any ideas?

Comments

B31 created an issue. See original summary.

sjancich’s picture

I have the same problem. The sort is correct when I do not have batched exports enabled. However, with batching, the export contains rows without my sort -- even though the parent sort option is selected.

I turned on the "show SQL queries" debug feature in views, and for my data export display, there is no ORDER BY clause in the query.

ankur.addweb’s picture

Can you try this with Views Data Export 7.x-3.1 ? Its working with me with exposed filters and batch process on.

sjancich’s picture

I still have the issue with 7.x-3.1.

kostask’s picture

I experienced a similar issue: The sort was working on the view and not on the views export.

I had this behaviour only on the production server (MariaDb) and not on my dev server (MySQL).

A temporary solution that applies the sort to the export was to add DISTINCT (Query settings: check Distinct) to the query.

antiorario’s picture

Version: 7.x-3.0-beta9 » 7.x-3.2
Component: User interface » Code

The sort bug on MariaDB is still present in 7.x-3.2. It's particularly nasty because Pantheon runs MariaDB, and I can only imagine how many large sites are potentially affected. The distinct trick works, but I'm afraid it won't be viable in all cases, so finding an actual solution would be best. I'm wondering whether this is a bug with MariaDB itself, and I'll try to poke around as soon as I have some time.

nitheesh’s picture

Can confirm the sort is working on Pantheon site when we apply distinct option.

GuyPaddock’s picture

Just ran into this myself on Pantheon.

Tunprog’s picture

Applying distinct fixes the issue for me. Thank you.

GuyPaddock’s picture

So, as @antiorario feared, I've just run into a situation where distinct is not an option -- Search API results.

We're trying to give our users the ability to export search results as CSV reports. Unfortunately, in this scenario, "distinct" isn't an option and the data does not come out in the same order as the search result page.

As it turns out, sorting does appear to work when exporting in Search API results, though still not 100%. The default sort for the search view doesn't seem to apply (i.e. if "Relevancy" is the default sort, among several other exposed sorts, then the results are not exported according to relevancy if the user does not change the sort). But, if I toggle to a different sorting option, or toggle away from the default and then back to it, sort apply. I tried using the option for "parent sort" but all that did was make the "Back to previous page" link appear on the batched export success page (which is desirable... but doesn't actually fix the sorting issue).

The reason I didn't think sorting worked was because I was using Better Exposed Filters with the "combined sorts" option (so that there is only one QS parameter for sorting instead of field and direction being separate QS parameters), while the export display of the view was using the standard Views exposed form. The two parse QS parameters differently, so it was ignoring the search parameters from the parent display.

GuyPaddock’s picture

Ok, I take it back. Sorting works correctly when exporting from a display that is attached to a Search API view. The reason why it didn't seem like the default sort of the parent display was having an effect was because the sort orders on the export display of the view weren't the same as the orders of the same sorts in the parent display.

Specifically, I had 'Relevance" set to sort DESC in the parent view and it was set to sort ASC in the export view by accident. That is why the export only matched the search if I explicitly set the sort order in the QS of the parent display because it would override the default incorrect sort in the export view.

JimCraner’s picture

I can confirm that the DISTINCT trick does NOT work for 7.x-3.2 when sorting by a Date field.

alesr’s picture

After losing some nerves with this I was finally able to find a solution with the combination of DISTINCT and hook_views_query_alter().

Just with hook_views_query_alter it didn't work because the query in batches doesn't care for ORDER BY.
Without the batching it worked fine. So the only thing that worked for me is to enable DISTINCT in this specific view display and adding hook_views_query_alter()

/**
 * Implements hook_views_query_alter().
 *
 * @param $view
 * @param $query
 */
function MYMODULE_views_query_alter(&$view, &$query) {
  if (($view->name == 'VIEWNAME') && $view->current_display == 'DISPLAYNAME') {
    $query->orderby[0]['field'] = "nid";
    $query->orderby[0]['direction'] = "ASC";
  }
}

Hope it helps anyone else ;)