Then one has to add a filter function remove names with a length of 0.
A script that will perform this task for any number of fields following you naming convention:
// get top level name field object;
var oName = this.getField("name");
// create an array of name fields below top level field object;
var aName = oName.getArray();
// repeat for price
var oPrice = this.getField("price");
var aPrice = oPrice.getArray();
// add names and price to array of values;
var aValues = new Array();
for(var i = 0; i < aName.length; i++)
{
aValues.push([aName.value.toString(), aPrice.value]);
}
// filter out items with a name of zero length;
function RemoveEmpty(element)
{
return (element[0].toString().length != 0);
} // end RemoveNull function;
aValues = aValues.filter(RemoveEmpty);
// sort by name, element 0 of item array;
function compare(a, b) {
if (a[0] < b[0]) {
return -1;
}
if (a[0] > b[0]) {
return 1;
}
// a must be equal to b
return 0;
} // end compare function;
// sort array of name and price values;
aValues.sort(compare);
// clear fields of values and reload using sorted values;
this.resetForm(aName.name, aPrice.name);
for(var i = 0; i < aValues.length; i++)
{
aName.value = aValues[0];
aPrice.value = aValues[1];
}
// done;