## Filter: wpd_ai_report_filters_users_should_sort

Control whether user results should be sorted when processing report filters. Disabling sorting can improve performance for very large datasets.

### Description

When fetching user data for reports, Alpha Insights can sort the results. For very large datasets, sorting can be expensive. This filter allows you to disable sorting to improve performance when order doesn't matter.

#### Location

File: `includes/classes/WPDAI_Report_Filters.php`

Method: `WPDAI_Report_Filters::get_users()`

Line: ~258

### Parameters

Parameter | Type | Description
--- | --- | ---
$should_sort | bool | Whether to sort user results (default: false)

#### Return

Type: bool

Description: True to enable sorting, false to disable

### Example Usage

### Enable Sorting for User Results

    add_filter( 'wpd_ai_report_filters_users_should_sort', '__return_true' );

### Conditional Sorting Based on Data Volume

    add_filter( 'wpd_ai_report_filters_users_should_sort', 'conditional_user_sorting' );
    function conditional_user_sorting( $should_sort ) {
        global $wpdb;

        // Get total user count
        $total_users = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->users}" );

        // Only sort if we have a manageable number of users
        if ( $total_users < 10000 ) {
            return true; // Enable sorting for smaller datasets
        }

        return false; // Disable sorting for large datasets
    }

### Enable Sorting for Specific User Roles

    add_filter( 'wpd_ai_report_filters_users_should_sort', 'sort_for_specific_contexts' );
    function sort_for_specific_contexts( $should_sort ) {
        // Enable sorting when viewing customer reports
        if ( isset( $_GET['user_type'] ) && $_GET['user_type'] === 'customer' ) {
            return true;
        }

        return $should_sort;
    }

### Best Practices

- Default is false (no sorting) for performance
- Enable sorting only if order matters for your reports
- Disable sorting for very large user bases (>10,000 users)
- Consider using client-side sorting for displayed results
- Test performance impact before enabling in production
- Sorting is most expensive with large datasets

### Performance Impact

With Sorting Enabled:

- Results are ordered (typically by user ID or name)
- More database processing required
- Slower for large datasets
- Better for reports where order matters

With Sorting Disabled (default):

- Faster query execution
- Less database load
- Results may appear in arbitrary order
- Better for large datasets where order doesn't matter

### Important Notes

- Default is false - sorting is disabled by default
- This only affects user data sorting, not other report data
- Disabling sorting improves performance for large user bases
- Results may appear in different order when sorting is disabled
- Consider using JavaScript sorting on the frontend if order matters
- This is a performance optimization filter

### When to Enable Sorting

Enable sorting if:

- You have a small user base (<10,000 users)
- Report order is important for your use case
- You're displaying results directly without client-side sorting
- Performance is acceptable with sorting enabled

Keep sorting disabled if:

- You have a large user base (>10,000 users)
- Report order doesn't matter
- You're using client-side sorting for display
- Performance is a concern

### Related Filters

- wpd_ai_report_filters_batch_size - Control report filter batch size
- wpd_ai_report_filters_is_transient_enabled - Control report caching

### Related Classes

- `WPDAI_Report_Filters` - Report filtering and processing

### Debugging

    add_filter( 'wpd_ai_report_filters_users_should_sort', 'debug_user_sorting', 999 );
    function debug_user_sorting( $should_sort ) {
        error_log( 'Alpha Insights User Sorting: ' . ( $should_sort ? 'ENABLED' : 'DISABLED' ) );
        return $should_sort;
    }