如果你想要纯 ObjectScript,你可以使用它;它假设你真的想做类似 Java 的事情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
}