4

Given:

2 strings strA, strB

I want:

To perform a comparison between them and return <0, =0 or >0, in Intersystems Cache ObjectScript.

So far:

I have found a function in the documentation that fulfills my needs StrComp. Unfortunately, this function is not part of Cache ObjectScript, but from Caché Basic.

I have wrapped the function as a classMethod of an utility class:

ClassMethod StrComp(
    pstrElem1 As %String,
    pstrElem2 As %String) As %Integer [ Language = basic ]
{
    Return StrComp(pstrElem1,pstrElem2)
}

Is this approach recommended? is there any function available?

Thanks in advance.

matmat
  • 85
  • 9

3 Answers3

3

It's a bit unclear exactly what you want this string comparison to do, but it appears that you're looking for either the follows ] or sorts after ]] operator.

Docs (taken from here):

  • The binary follows operator (]) tests whether the characters in the left operand come after the characters in the right operand in ASCII collating sequence.
  • The binary sorts after operator (]]) tests whether the left operand sorts after the right operand in numeric subscript collation sequence.

The syntax looks weird but it should do what you need.

if "apple" ] "banana" ...
if "apple" ]] "banana" ...
Brandon Horst
  • 1,921
  • 16
  • 26
  • Yes, using that operator I can write a function that first compares both strings and returns 0 if they are equal. If they are different I use your suggested operator and return 1 or -1 if strA "sorts after" strB – matmat Jun 08 '15 at 06:35
  • @matmat you may also want to see my solution, since it walks all the strings only once; however it may be improved – fge Jul 28 '15 at 12:05
  • Unfortunately, the documentation is incorrect! (I actually implemented those operators when I worked at InterSystems). – Scott Jones Dec 26 '16 at 20:21
  • `]` will compare based on the 8-bit characters or the 16-bit UCS-2 characters (i.e. non-BMP Unicode characters, i.e. those > 0xffff will sort based on their representation as a pair of surrogate characters). – Scott Jones Dec 26 '16 at 20:31
  • `]]` will compare based on the current collation sequence setting. By default, that will sort "" first, then all numbers (and *canonic numeric* strings), and finally all other strings. – Scott Jones Dec 26 '16 at 20:32
2

You can use that if you want pure ObjectScript; it supposes that you really want to do something like Java's Comparable<String>:

///
/// Compare two strings as per a Comparator<String> in Java
///
/// This method will only do _character_ comparison; and it pretty much
/// assumes that your Caché installation is Unicode.
///
/// This means that no collation order will be taken into account etc.
///
/// @param o1: first string to compare
/// @param o2: second string to compare
/// @returns an integer which is positive, 0 or negative depending on
/// whether o1 is considered lexicographically greater than, equal or
/// less than o2
ClassMethod strcmp(o1 as %String, o2 as %String) as %Integer
{
    #dim len as %Integer
    #dim len2 as %Integer
    set len = $length(o1)
    set len2 = $length(o2)
    /*
     * Here we rely on the particularity of $ascii to return -1 for any
     * index asked within a string literal which is greater than it length.
     *
     * For instance, $ascii("x", 2) will return -1.
     *
     * Please note that this behavior IS NOT documented!
     */
    if (len2 > len) {
        len = len2
    }

    #dim c1 as %Integer
    #dim c2 as %Integer

    for index=1:1:len {
        set c1 = $ascii(o1, index)
        set c2 = $ascii(o2, index)

        if (c1 '= c2) {
            return c1 - c2
        }
    }

    /*
     * The only way we could get here is if both strings have the same
     * number of characters (UTF-16 code units, really) and are of
     * equal length
     */
    return 0
}
fge
  • 119,121
  • 33
  • 254
  • 329
  • 1
    This is a more OO solution! I was looking for the kind of solution provided by @BrandonHorst, but this answer is great, thanks @fge! ;) – matmat Jul 30 '15 at 11:08
1

It is possible to use different languages in your code, if it solve your task, why not. But you have to notice that not all languages is works on server's side. JavaScript still client's side language, and can't be used in such way.

DAiMor
  • 3,185
  • 16
  • 24