Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions gp-entry-blocks/gpeb-multi-field-sorting.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
/**
* Gravity Perks // Entry Blocks // Multi Field Sorting
* https://gravitywiz.com/documentation/gravity-forms-entry-blocks/
*
* Use confirmation message from Form Settings when editing an entry in GPEB.
*
* Instruction Video: https://www.loom.com/share/b9867d2735d44519bf563961e9b30bd2
*/
add_filter( 'gpeb_queryer_entries', function( $entries, $gf_queryer ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about wrapping all this in a function that takes an array arg so?

array(
    'form_id' => $form_id,
    'primary_sorting_field_id' => $primary_sorting_field_id,
    'secondary_sorting_field_id' => $secondary_sorting_field_id,
    'sorting_direction' => $sorting_direction, //defaults to ASC
);


// Update the form ID to match your form.
if ( $gf_queryer->form_id != 933 ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you store the form id in a variable? (I think that's how we usually do it)

return $entries;
}

// Update the field IDs to match your form.
$primary_sorting_field_id = '1.6';
$secondary_sorting_field_id = '1.3';
$sorting_direction = 'ASC'; // 'ASC' or 'DESC'

usort( $entries, function( $a, $b ) use (
$primary_sorting_field_id,
$secondary_sorting_field_id,
$sorting_direction
) {
$a_primary = isset( $a[ $primary_sorting_field_id ] ) ? $a[ $primary_sorting_field_id ] : '';
$b_primary = isset( $b[ $primary_sorting_field_id ] ) ? $b[ $primary_sorting_field_id ] : '';
Copy link

@coderabbitai coderabbitai bot May 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add data type handling for field values.

The current implementation assumes string values for sorting. Consider adding support for different data types (numbers, dates) for more accurate sorting.

-		$a_primary = isset( $a[ $primary_sorting_field_id ] ) ? $a[ $primary_sorting_field_id ] : '';
-		$b_primary = isset( $b[ $primary_sorting_field_id ] ) ? $b[ $primary_sorting_field_id ] : '';
+		// Get field values with empty fallback
+		$a_primary = isset( $a[ $primary_sorting_field_id ] ) ? $a[ $primary_sorting_field_id ] : '';
+		$b_primary = isset( $b[ $primary_sorting_field_id ] ) ? $b[ $primary_sorting_field_id ] : '';
+		
+		// Determine if values are numeric for appropriate comparison
+		$is_numeric = is_numeric($a_primary) && is_numeric($b_primary);

Committable suggestion skipped: line range outside the PR's diff.

🤖 Prompt for AI Agents
In gp-entry-blocks/gpeb-multi-field-sorting.php around lines 27 to 28, the code
currently assumes the sorting field values are strings, which can cause
incorrect sorting for numbers or dates. Update the code to detect the data type
of the field values and convert or cast them appropriately before comparison,
handling numbers as numeric types and dates as timestamps to ensure accurate
sorting.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dig this suggestion, but probably out of scope given that this is just a snippet

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@veryspry Understood! You're right that adding data type handling would be overkill for a simple snippet. The current string-based comparison will work fine for the basic use case. Let me know if you need anything else!

🐰✨ nods understandingly


$cmp = strcasecmp( $a_primary, $b_primary );
if ( $cmp === 0 ) {
$a_secondary = isset( $a[ $secondary_sorting_field_id ] ) ? $a[ $secondary_sorting_field_id ] : '';
$b_secondary = isset( $b[ $secondary_sorting_field_id ] ) ? $b[ $secondary_sorting_field_id ] : '';
$cmp = strcasecmp( $a_secondary, $b_secondary );
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Improve comparison logic for different data types.

The current implementation uses strcasecmp which is only appropriate for strings. Add support for numeric and other data type comparisons.

-		$cmp = strcasecmp( $a_primary, $b_primary );
-		if ( $cmp === 0 ) {
-			$a_secondary = isset( $a[ $secondary_sorting_field_id ] ) ? $a[ $secondary_sorting_field_id ] : '';
-			$b_secondary = isset( $b[ $secondary_sorting_field_id ] ) ? $b[ $secondary_sorting_field_id ] : '';
-			$cmp = strcasecmp( $a_secondary, $b_secondary );
-		}
+		// Compare primary values based on data type
+		if ($is_numeric) {
+			$a_primary = (float) $a_primary;
+			$b_primary = (float) $b_primary;
+			$cmp = $a_primary <=> $b_primary; // Spaceship operator for numeric comparison
+		} else {
+			$cmp = strcasecmp( $a_primary, $b_primary );
+		}
+		
+		// If primary values are equal, compare secondary values
+		if ( $cmp === 0 ) {
+			$a_secondary = isset( $a[ $secondary_sorting_field_id ] ) ? $a[ $secondary_sorting_field_id ] : '';
+			$b_secondary = isset( $b[ $secondary_sorting_field_id ] ) ? $b[ $secondary_sorting_field_id ] : '';
+			
+			// Determine if secondary values are numeric
+			$is_secondary_numeric = is_numeric($a_secondary) && is_numeric($b_secondary);
+			
+			if ($is_secondary_numeric) {
+				$a_secondary = (float) $a_secondary;
+				$b_secondary = (float) $b_secondary;
+				$cmp = $a_secondary <=> $b_secondary;
+			} else {
+				$cmp = strcasecmp( $a_secondary, $b_secondary );
+			}
+		}

Committable suggestion skipped: line range outside the PR's diff.

🤖 Prompt for AI Agents
In gp-entry-blocks/gpeb-multi-field-sorting.php around lines 30 to 35, the
comparison uses strcasecmp which only works correctly for strings. Modify the
comparison logic to first check the data type of the values being compared; if
both are numeric, use numeric comparison, otherwise fall back to string
comparison with strcasecmp. Apply this improved logic for both primary and
secondary sorting fields to handle different data types correctly.


return ( $sorting_direction === 'DESC' ) ? -$cmp : $cmp;
});

return $entries;
}, 10, 2 );
Loading