sort and sortWith perform string comparisons by default. Use the
NumericComparator class to sort the strings as numbers.

The sections of the reference you are citing are for the different string
comparison operators. sort and sortWith do not use those operators, but
rather the compareTo method which is purely a string comparison.

Rick

On Mon, May 14, 2018 at 12:59 PM, Leslie Turriff <[email protected]> wrote:

>         I need to sort an array of strings by their signed numeric
> suffixes, but I'm
> getting some strange results from sort and sortWith.  I have attached my
> V4.2
> test program; together with its output.
>
>         Section 5.1.3.8 of the Language Reference says,
> 'The strict comparison operators do not attempt to perform a numeric
> comparison on the two operands.
> For all the other comparison operators, if both terms are numeric, the
> String
> class does a numeric comparison (ignoring, for example, leading zeros— see
> Section 10.4, “Numeric Comparisons”).'
>
>         Section 10.4 says,
> 'Numeric values are compared by subtracting the two numbers (calculating
> the
> difference) and then comparing the result with 0. That is, the operation:
>    A ? Z
> where ? is any numeric comparison operator, is identical with:
>    (A - Z) ? "0"
> It is, therefore, the difference between two numbers, when subtracted
> under
> Rexx subtraction rules, that determines their equality.'
>
>         Further, Section 10 says,
> 'Numbers can be expressed flexibly. Leading and trailing whitespace
> characters
> are permitted, and
> exponential notation can be used. Valid numbers are, for example:
>
>   Example 10.1. Numbers
>
>    12               /* a whole number                         */
>    "-76"            /* a signed whole number                  */
>      12.76             /* decimal places                      */
>      " + 0.003 "       /* blanks around the sign and so forth */
>      17.               /* same as 17                          */
>      .5                /* same as 0.5                         */
>      4E9               /* exponential notation                */
>      0.73e-7           /* exponential notation                */
>
> A number in Rexx is defined as follows:
>
>   >>-+------------+--+----------------------+--+-digits-------
> -+---------->
>       +-whitespace-+ +-sign--+------------+-+ +-digits.digits-+
>                                  +-whitespace-+    +-.digits-------+
>                                                    +-digits.-------+
>   >--+------------+--------------------------------------------------><
>       +-whitespace-+
>
> whitespace
>      are one or more blanks or horizontal tab characters.
> sign
>      is either + or -.
> digits
>      are one or more of the decimal digits 0-9.'
>
>         However, in section 5.3.18, Sorting Arrays, we see that
> 'The sort method orders the strings by using the compareTo method of the
> String class. The compareTo method knows how to compare one string to
> another, and returns the values -1 (less than), 0 (equal), or 1 (greater
> than) to indicate the relative ordering of the two strings.'
>
> and
> 'Performs a sort comparison of the target string to the string argument.
> If
> the two strings are equal, 0 is returned. If the target string is larger,
> 1
> is returned. -1 if the string argument is the larger string.
> The comparison is performed starting at character n for length characters
> in
> both strings. n must be a positive whole number. If n is omitted, the
> comparison starts at the first character. length must be a non-negative
> whole
> number. If omitted, the comparison will take place to the end of the
> target
> string.'
>
> which seems to imply a character comparison; and the examples for
> compareTo
> include no numeric strings.
>
>         Looking at the output from the test program, ooRexx seems to be
> sorting
> signed numeric strings non-numerically (+ < - < 0), contrary to what would
> be
> expected from section 10.  This seems to me to be a bug?  I would not
> expect
> to have to write a custom comparator to re-implement a built-in mechanism.
>
> Leslie
>
>
>
>
> ------------------------------------------------------------
> ------------------
> Check out the vibrant tech community on one of the world's most
> engaging tech sites, Slashdot.org! http://sdm.link/slashdot
> _______________________________________________
> Oorexx-devel mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/oorexx-devel
>
>
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Oorexx-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/oorexx-devel

Reply via email to