Björn, generally strings are compared by pairing each character and
determining which of the pair has a greater byte value. Yes, this gets
complicated for Unicode vs ASCII, but as with numbers, you need to convert
one side or the other "up" in precision until the types match and you have
an objective basis for comparison.

J doesn't seem to provide the standard ord/chr functions, though it does
provide a rather complicated version that can be used to get the same.

ord =: 3 & u:

chr =: 4 & u:

Then you iterate until one side's byte value is lower than the other's,
returning _1 if the left side is lower or 1 if the right side is lower; or
when one side has no more characters, returning _1 if the left side is
shorter or 1 if the right side is shorter; or when both sides run out of
characters and each past pair has matched, return 0 (equal).

Cheers,

Andrew Pennebaker
www.yellosoft.us

2011/10/30 Björn Helgason <[email protected]>

> How would you decide which string is bigger?
>
>    3 u: 7 u: 'ӒAÁú'
> 1234 65 193 250
>    3 u: 7 u: 'abssdgasfgas'
> 97 98 115 115 100 103 97 115 102 103 97 115
>    +/3 u: 7 u: 'ӒAÁú'
> 1742
>    +/3 u: 7 u: 'abssdgasfgas'
> 1257
>
> 2011/10/30 Don Guinn <[email protected]>
>
> > 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
> >
>
>
>
> --
> Björn Helgason, Verkfræðingur
> Fornustekkum II
> 781 Hornafirði,
> t-póst: [email protected]
> gsm: +3546985532
> twitter: @flugfiskur
> http://groups.google.com/group/J-Programming
>
>
> Tæknikunnátta höndlar hið flókna, sköpunargáfa er meistari einfaldleikans
>
> góður kennari getur stigið á tær án þess að glansinn fari af skónum
>          /|_      .-----------------------------------.
>         ,'  .\  /  | Með léttri lund verður        |
>     ,--'    _,'   | Dagurinn í dag                     |
>    /       /       | Enn betri en gærdagurinn  |
>   (   -.  |        `-----------------------------------'
>   |     ) |         (\_ _/)
>  (`-.  '--.)       (='.'=)   ♖♘♗♕♔♙
>   `. )----'        (")_(") ☃☠
> ----------------------------------------------------------------------
> 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