You might have a need for the title sort field to be writable, like copying the "title" field to the "title sort" field, BUT I think you don't need that in which case you can use a JavaScript (JS) field instead of a trigger, so that's my recommendation.
My 1st thought is that this problem has been addressed & solved millions of times by database authors, so try search keys like "javascript move the to end". If you don't find something good enough there, you can do it yourself, if you know some String object methods (object functions. Try "javascript methods trim substring" or something like that.
In general, the #1 place for info on all things JavaScript is the JS Web Docs pages at Mozilla. I generally just Google "MDN JavaScript", (MDN stands for Mozilla Developer Network.) but it takes you for the time being to...
https://developer.mozilla.org/en-US/docs/Web/javascript
...so if you like URLs, go there. I suggest doing this on a PC; mobile browsers tend to be way too limiting.
The left side of the page (in a PC browser) is where it lists methods, including String object methods trim and substring, so scroll down the alphabetical list to find substring first & then trim. In Memento, a Text field's value is a JS String object, so I think you want to use a few String methods to test the "title" field for a leading "The " (skip any leading blanks); if you don't find it in an entry, just use the value of the "title" field for that of the "title sort" field; but if you do find a leading "The " in an entry, you have a bit of work to do...
First, trim the title of any leading or trailing white space (blanks, mainly) by using the trim method. The MDN page for trim says...
The trim() method removes whitespace from both ends of a string. Whitespace in this context is all the whitespace characters (space, tab, no-break space, etc.) and all the line terminator characters (LF, CR, etc.).
The 1st script line could be ("//" means the rest of the line is a comment to you)...
// The () below are for arguments (parameters).
// field has 1 argument "title",
// but trim doesn't have any arguments,
// but it still needs the empty ().
// var defines a variable, which is a value with a name.
// tsort will have the value of that field returns to it,
// which is what "title" has with blanks trimmed off.
var tsort = field("title").trim(); // Finally, the first line of JS code! Does it make sense now?
The next thing is to check the 1st 4 characters to see if it matches "The ". If it doesn't match, we will just return either the value of the variable tsort or the value of the field "title". I recommend tsort's value; it keeps all the data more normalized or standard in format, but if for some reason the value of the "title" field always has to match the value in the source of the data, go for the value of the "title" field, since you must.
Stripping of the leading "The " is easy -- just use the substring method. Putting it onto the back end of the title is also just as easy. The MDN page for substring is longer. It says…
substring() extracts characters from indexStart up to but not including indexEnd. In particular:
If indexEnd is omitted, substring() extracts characters to the end of the string.
If indexStart is equal to indexEnd, substring() returns an empty string.
If indexStart is greater than indexEnd, then the effect of substring() is as if the two arguments were swapped; See example below.
Any argument value that is less than 0 or greater than stringName.length is treated as if it were 0 and stringName.length, respectively.
So, the next code we need will do the test and decide which of 2 things to do based on that.
// To deal with The, the, THE, tHe, and so on,
// we convert everything for testing to lower case,
// though the library fields still have the original case.
// substring(0,4) returns the 1st 4 characters of tsort.
// toLowerCase returns the 1st 4 characters of tsort
// shifted to lower case.
// == tests the left side to the right side
// if evaluates the value returned by the == operator.
// If it is true, the 1st code block is executed.
// Otherwise, the 2nd block is executed instead.
if (tsort.substring(0,4).toLowerCase() == “the “) { // Matches
tsort = tsort.substring(4) + “, “ + tsort.substring(0, 3);
}
else { // No match
tsort; // Return the value trimmed, case unchanged.
// For untrimmed, return field(“title”) instead.
}
We are done. We returned one value for a match & another for no match. Put the 2 chunks of code together in a JS field called “title sort”, and you have it. I hope I don’t have any mistakes above; I have not tested it.
I’d love to hear feedback on this from you or from anyone. It’s a nice little JS instructional piece, which is what attracted me to it. I’ll be putting it into the wiki or at least into a Bill’s mementos newsletter, so please help me make sure it’s right and as good as it can be. Thanks in advance.
--
You received this message because you are subscribed to the Google Groups "mementodatabase" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mementodataba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mementodatabase/4dcdc0eb-75ba-4d12-b82d-f99f661a240an%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mementodatabase/f1071eeb-3f95-42ea-9f86-7cfdcbb9b42an%40googlegroups.com.