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



#!/usr/bin/rexx
  trace normal
  say 'Sort an array on (signed numeric) columns 5-6 using the "sortWith" 
method.'
  testWith = .Array ~ of('test00','test+1','test-2','test+2','test-1')
  say '  Before:'

  do t = 1 to testWith ~ items
    say testWith[t]
  end

  testWith ~ sortWith(.ColumnComparator ~ new(5,2))
  say
  say '  After:'

  do t = 1 to testWith ~ items
    say testWith[t]
  end

  say
  say 'Sort an array on all (signed numeric) columns using the "sort" method.'
  say '  Before:'

  test = .Array ~ of('00','+1','-2','+2','-1')

  do t = 1 to testWith ~ items
    say test[t]
  end

  test ~ sort
  say
  say '  After:'

  do t = 1 to test ~ items
    say test[t]
  end

  say 'Sort an array on (signed numeric) columns 5-6 using the "stableSortWith" 
method.'
  testWith = .Array ~ of('test00','test+1','test-2','test+2','test-1')
  say '  Before:'

  do t = 1 to testWith ~ items
    say testWith[t]
  end

  testWith ~ stableSortWith(.ColumnComparator ~ new(5,2))
  say
  say '  After:'

  do t = 1 to testWith ~ items
    say testWith[t]
  end

  say
  say 'Sort an array on all (signed numeric) columns using the "stableSort" 
method.'
  say '  Before:'

  test = .Array ~ of('00','+1','-2','+2','-1')

  do t = 1 to testWith ~ items
    say test[t]
  end

  test ~ stableSort
  say
  say '  After:'

  do t = 1 to test ~ items
    say test[t]
  end
exit
Sort an array on (signed numeric) columns 5-6 using the "sortWith" method.
  Before:
test00
test+1
test-2
test+2
test-1

  After:
test+1
test+2
test-1
test-2
test00

Sort an array on all (signed numeric) columns using the "sort" method.
  Before:
00
+1
-2
+2
-1

  After:
+1
+2
-1
-2
00
Sort an array on (signed numeric) columns 5-6 using the "stableSortWith" method.
  Before:
test00
test+1
test-2
test+2
test-1

  After:
test+1
test+2
test-1
test-2
test00

Sort an array on all (signed numeric) columns using the "stableSort" method.
  Before:
00
+1
-2
+2
-1

  After:
+1
+2
-1
-2
00
------------------------------------------------------------------------------
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