Sort Nested Form Entries

Instructions

  1. Install this snippet with our free Custom JavaScript plugin. https://gravitywiz.com/gravity-forms-code-chest/

Code

Filename: gpnf-sort-nested-form-entries.js

/**
 * Gravity Perks // Nested Forms // Sort Nested Form Entries
 *
 * https://gravitywiz.com/documentation/gravity-forms-nested-forms/
 *
 * Instructions:
 *
 * 1. Install this snippet with our free Custom JavaScript plugin.
 *    https://gravitywiz.com/gravity-forms-code-chest/
 */
// Change this value to the field ID of the child form you want to sort by.
const sortByFieldId = "3";
window.gform.addFilter('gpnf_sorted_entries', function (entries, formId, fieldId, gpnf) {
	// Check if entries exist and have the specified field.
	if ( !entries || !entries.length || !entries[0][sortByFieldId]) {
		console.warn(`GPNF Sort: Field ID ${sortByFieldId} not found in entries or entries are empty. Returning unsorted entries.`);
		return entries;
	}

	// Sort entries by the specified field's label.
	return entries.sort((a, b) => {
		if ( !a[sortByFieldId] || !a[sortByFieldId].label) return 1;
		if ( !b[sortByFieldId] || !b[sortByFieldId].label) return -1;
		return a[sortByFieldId].label.localeCompare(b[sortByFieldId].label);
	});
});

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.