Jump to content

Arrays, more arrays & sorting?


esmith

Recommended Posts

Let's say I have a record with the following, comma-delimited fields:

name-1,price-1,name-2,price-2,name-3,price-3,name-4,price-4
"Apple","3.00","Banana","1.50","","","Durant","2.75"

 

I want to create a rule that adds the non-null values of name-1 thru name-4 and price-1 thru price-4 to an array, sorts the related values by price, and spits out the result as formatted text.

 

I've considered an array of arrays which would populate via a FOR loop designed to ignore empty values:

product[name["Apple", "Banana", "Durant"],price["3.00", "1.50", "2.75"]]

which would then sort both [name] and [price] by price:

product[name["Banana", "Durant", "Apple"],price["1.50", "2.75", "3.00"]]

 

This is how I have tried to set up my code (not including sorting):

var pname = [];
var price = [];
var products = [pname, price];
for (var i=1; i<5; i++) {
  var currProduct = "name_" + i;
  var currPrice =  "price_" + i;
  if (Field(currProduct) != "") {
     pname.push(ToUpper(Field(currProduct)));
     if (Field(currPrice) != "") {
        price.push(Field(currPrice));
     } else price.push("");
  } 
}
// debug code to see if arrays populated correctly
// return price[0] correctly returns "3.00"
// return products[price][0] returns error: [price] does not have any properties

// sort values of both pname and price based on price data

// return list with tagged formatting

 

Assuming I can get the above code to work, I still have no idea how to sort both price and name based on the price. Could someone more intelligent than me (you all qualify) offer some guidance on how to proceed with this dilemma?

Link to comment
Share on other sites

Eric,

 

Maybe this will be cleaner than the scribbled notes I gave you in person. My code assumes the outer array is the overall product list and each item is an array of product name and price.

 

 

var products = []; // Product List

for (var i=1; i<5; i++)
{
   //Grab and format your data as you want
   var currProduct = Field("name-" + i);
   var currPrice = Field("price-" + i);

   //Each item as an array (of length 2) is added to Product List
   products.push([currProduct, currPrice]);
}


//Testing the before sort results
var returnResults = "***Before Sort:\n";
for (var i=0; i < products.length; i++)
{
   returnResults += products[i][0] + "......." + products[i][1];
   returnResults += "\n";
}


//Callback function to use with sort
//Essentially, describes how to sort 2 "items"
var sortProd = function(a,b) {
   if(a[1] < b[1]) return -1;
   if(a[1] > b[1]) return 1;
   if(a[1] == b[1]) return 0;
}

//Sort the Product List using the callback as an argument
products.sort(sortProd);

//Testing the after sort results
returnResults += "\n\n***After Sort:\n";

for (var i=0; i < products.length; i++)
{
   returnResults += products[i][0] + "......." + products[i][1];
   returnResults += "\n";
}

//Returning the Test results or formatted text or whatever
return returnResults;

Link to comment
Share on other sites

Thanks for that solution David. I had to make a minor tweak since your sortProd variable sorted as a string rather than as a number (1120 was coming before 930 because 1<9). I replaced sortProd with the following:

var sortProd = function(a,b) {
   return a[1] - b[1];
}

and now I have everything working perfectly. By the way, the company you work for seems to have several knowledgable and helpful FusionPro users. ;)

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...