Post

3 followers Follow

Trouble with Sort Function In Calculation Field

Hi,

I am creating a fitness application and am utilizing a simple calculation field to create a Grocery List.  In the below code, "@all of meal portion list" is referencing an identifier that includes the name of the ingredient and the quantity for all related ingredients.

Grocery List Output

var list = @All of Meal Portion List;
var array = [];

for (i=0; i<list.length; i++){

array.push(list[i]);

}

array.sort().join("\n")

This is the output:

Chicken, breast, skinless, cooked X 1
Pepper, Sweet, Red, Raw X 0.5
Organic Carrots, raw X 1
Celery, Raw X 3
quinoa, organic X 1
Greek Yogurt, Chobani X 1
Almond Butter X 1
Kroger Orange Juice X 1
Thrive 6 - Mushroom Brain Supplement X 1
Purely Inspired Organic Protein X 1
Less Naked Mass X 1
Organic Eggs, Large X 3
Organic Egg White X 3
Kroger Coconut Oil X 1
Sirloin tip steak choice beef round X 1
Pepper, Sweet, Red, Raw X 0.5
Organic Carrots, raw X 1
Celery, Raw X 3
quinoa, organic X 1

Any ideas why the items aren't sorted?

Jon Licht

Hi Jon,

I assume you are creating a list of lists. In the field List (in the related app) you have lists like

Chicken, breast, skinless, cooked X 1
Pepper, Sweet, Red, Raw X 0.5
Organic Carrots, raw X 1
Celery, Raw X 3
quinoa, organic X 1

and

Sirloin tip steak choice beef round X 1
Pepper, Sweet, Red, Raw X 0.5
Organic Carrots, raw X 1
Celery, Raw X 3
quinoa, organic X 1

Correct?

For the sort() function the whole content of an array element (= list[i]) is one string and it sorts alphabetically for the whole string, not for each ingredient (you can test that: in your last line change .join("\n") to .join("\n---\n" and you'll see where a list]i] string starts and ends). This means you have to split each list (string) into it's ingredients first,then create an array of all ingredieants and  then you can sort for all ingredients from all lists. But that's rather simple and you don't need a for loop:

`@All of Meal Portion List.join("\n").split("\n").sort().join("\n")`

But be aware that the sort function sorts for strings with capital letters first, then for lower case strings. Means "quinoa, organic X 1" will always be at the end of the list, not between "Purely Inspired Organic Protein X 1" and  "Sirloin tip steak choice beef round X 1".

In the sorted list you will have duplicates e.g. "Pepper, Sweet, Red, Raw X 0.5" and "quinoa, organic X 1". If you want them only once in the list:

`var arr = @All of Meal Portion List.join("\n").split("\n").sort();var arrUnique = arr.filter(function(item, pos) {return arr.indexOf(item) == pos;});arrUnique.join("\n")`

Rainer

Rainer Grabowski 1 vote

Rainer, you are the man.  Thanks.  That worked perfectly.  That was a simple fix.

No big deal if you don't have time for this next one, but any idea how I would consolidate the quantities?

The above Training Cycle Item is Referenced by Meal Schedule Items.  A Meal Schedule Item is constituted of a list of Meal Portion Items.  A meal portion item references a single ingredient and has a quantity field.  The identifier is  (Ingredient - Serving Size X Quantity).

So, for a given Grocery List output in a training cycle item, If (Ingredient - Serving Size) for an item within the array is equal to another (Ingredient - Serving Size), those two items are equivalent and their quantities can be combined and listed as a single array item.

I think that means that I can simply split each identifier using the "X" symbol then add the quantities together.  I'm not sure how I would do that in JavaScript, though.

Hi Jon,

This looks like a great app! I have been looking to create something like this. Is there a possibility you would share the app?

Duane,

Sure thing.  I would love some professional feedback actually.  The applications I am putting together are works-in-progress that I use to manage my personal life.

* fitness

* Diet

* vehicles

* assets

* goals

* Daily Battle Rhythm

* etc...

It helps keep my skills sharp as I get paid to do the same thing on a much larger scale for the company I work for.  I would be open to sharing both workspaces with you.  It might not be intuitive to use, though.  I set it up for me, not for mass distribution.

I would be open to having a short call with you to go through it, then I can share it to the App Market.

My single-use link is here: https://calendly.com/s/10Yosy9R