Hello,
I have searched this forum on @sort and am not finding what I need help with.
I have a field that concatenates 2 fields, for example
shirt + blue
pants + red
socks + blue
belt + red
I need to sort this list so that all the blue items display at the top of the list, and the red items display at the bottom of the list, but I need to maintain the order the user entered the items in.
My concat field is sorting all the elements in the list by putting the color before the item name and sorting alphabetically.
Then my display field loops through the list and puts the item name in front and color in back.
This however, does not maintain the order in which the user entered the items as it sorts the whole string, color and item, alphabetically.
here is the formula for my concat field:
@For(n := 1; n <= @Elements(ItemName); n := n+1;
FIELD concat := @If(n = 1; @Sort( “(” + color[n] + ") " + ItemName[n] ) ;
@Sort(concat : (“(” + color[n] + ") " + ItemName[n]) )) );
@Sort(concat)
This gives me the following result:
(blue) shirt
(blue) socks
(red) belt
(red) pants
So my display fields swaps them around to look like this:
shirt (blue)
socks (blue)
belt (red)
pants (red)
So now I need to maintain the list in the order the user entered them.
What do I do now to give me the following result:
shirt (blue)
socks (blue)
belt (red)
pants (red)
Subject: RE: @sort, @for, need help with multiple sort text list
Why don’t you make the list position part of the sorting list?
@For(n := 1; n <= @Elements(ItemName); n := n+1;
tmp := color[n] + "/ " + @Right(“00”+@text(n); 3) + “.” + ItemName[n] )
concat := @If(n = 1; tmp ; concat : tmp)
);
sorted := @Sort(concat);
FIELD color := @Left(sorted; “/”);
FIELD ItemName := @Right(sorted; “.”);
Incidentally, there’s no need to store the value in a field each time thru the loop (or at all). Just use temporary variables and only store your final result in fields. And of course, do not do the expensive sort operation each time through your loop.
Subject: RE: @sort, @for, need help with multiple sort text list
the list of values in the concat field (ItemName field + Color field) are stored because the ItemName field and Color fields in the form are not sorted by ordered entered and color, but only by order entered regardless of color.
The concat field sorts and stores a list to be used to populate a field on a response document. So on creation of the response I have a field that gets me the list from the concat field sorted by color, not by order entered.
I don’t know how to do both.
Subject: RE: @sort, @for, need help with multiple sort text list
So you are inheriting field values into a response document. Fine; the ItemName and Color fields are available to formulas as you’re composing the response document, then. So you have (let’s say) a Computed when composed field that uses the formula I posted, up to the point where it starts assigning fields. At that point, you could instead just write:
“(” + @Left(sorted; “/”) + ") " + @Right(sorted; “.”)
No need to store this value in the main document when you can calculate it so easily as needed. Less items stored = better performance.