By "string" I assume you mean "literal". = only gives equal or not equal
for literals. > and like give domain errors for literals. However, grade
(/:) does perform a sort on literals.

ASCII literals work pretty well with grade; however, if any Unicode are in
the literal things get messy. When I need to sort literals that may include
Unicode I use the u: verb as below. This also converts the literal to
numeric so all comparisons work.

   3 u: 7 u: 'ӒA'
1234 65

On Sun, Oct 30, 2011 at 2:58 PM, Andrew Pennebaker <
[email protected]> wrote:

> I made a general cmp function for arbitrary data types. Let me know if J
> has a built-in function with the same API.
>
> https://github.com/mcandre/mcandre/blob/master/j/cmp.j
>
> Example usage:
>
>   1 cmp 2
> -1
>   2 cmp 1
> 1
>   1 cmp 1
> 0
>   'abc' cmp 'def'
> -1
>   'def' cmp 'abc'
> 1
>   'abc' cmp 'abc'
> 0
>   1 lt 2
> 1
>   2 lt 1
> 0
>   1 lte 2
> 1
>   1 lte 1
> 1
>   'abc' gte 'def'
> 0
>   'abc' gte 'abb'
> 1
>
> Cheers,
>
> Andrew Pennebaker
> www.yellosoft.us
>
> On Sun, Oct 30, 2011 at 4:29 PM, Andrew Pennebaker <
> [email protected]> wrote:
>
> > Is there a dyad that returns -1 for string x less than string y, 1 for x
> > greater than y, and 0 for x equals y?
> >
> > -: Almost does this, but it only tests equality; it's not specific enough
> > to tell you whether x or y is greater. Instead it just returns 0 for not
> > equal and 1 for equal.
> >
> > The compare dyad almost does this, but with an API with complexity on the
> > order of diff or subversion.
> >
> >    'abc' compare 'def'
> > 0 [0] abc
> > 1 [0] def
> >
> >    'abc' compare 'abc'
> > no difference
> >
> > In a conditional you would have to use (('abc' compare 'abc') compare 'no
> > difference') compare 'no difference' ad infinitum just to see if there
> > was 'no difference' in the original compare!
> >
> > Cheers,
> >
> > Andrew Pennebaker
> > www.yellosoft.us
> >
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
>
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to