Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4095737

Array.sort(String[]) still behaves funny

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P3 P3
    • None
    • 1.2.0
    • core-libs
    • x86
    • windows_95



      Name: ccC48265 Date: 11/27/97

      =20
      This example shows that Array.sort(String[]) behaves strangely,
      sorting all the uppercase letters first, then the lowercase letters. Try
      it and you'll see. Maybe I'm missing something but this doesn't seem
      to be the traditional way to sort strings. Is this what is intended?

      //: Array1.java
      // Testing the sorting & searching in Array
      // package c08.newcollections;
      import java.util.*;
      public class Array1 {
       static final int SIZE =3D 5;
       static Random r =3D new Random();
       static String ssource =3D=20
       "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
       "abcdefghijklmnopqrstuvwxyz";
       static char[] source =3D ssource.toCharArray();
       static String randString() {
       char[] buf =3D new char[SIZE];
       int rchoice;
       for(int i =3D 0; i < SIZE; i++) {
       rchoice =3D=20
       (int)(Math.random() * source.length);=20
       buf[i] =3D source[rchoice];
       }
       return new String(buf);
       }
       public static void print(byte[] b) {
       for(int i =3D 0; i < b.length; i++)
       System.out.print(b[i] + " ");
       System.out.println();
       }
       public static void print(String[] s) {
       for(int i =3D 0; i < s.length; i++)
       System.out.print(s[i] + " ");
       System.out.println();
       }
       public static void main(String[] args) {
       byte[] b =3D new byte[15];
       r.nextBytes(b);
       print(b);
       Array.sort(b);
       print(b);
       int loc =3D Array.binarySearch(b, b[10]);
       System.out.println("Location of " + b[10] +
       " =3D " + loc);
       String[] s =3D new String[10];
       for(int i =3D 0; i < s.length; i++)
       s[i] =3D randString();
       print(s);
       Array.sort(s);
       print(s);
       loc =3D Array.binarySearch(s, s[4]);
       System.out.println("Location of " + s[4] +
       " =3D " + loc);
       }
      } ///:~

      =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
      =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
      =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
      ###@###.### -- 97-11-27 :

      I tested the above program with JDK1.2beta1 (P2) and here is some
      sample output (lines of interest are indicated by '->'):

      =09java Array1
      =09-50 -34 76 -93 -117 104 109 -112 24 -98 24 -28 48 95 -38=20
      =09-117 -112 -98 -93 -50 -38 -34 -28 24 24 48 76 95 104 109=20
      =09Location of 48 =3D 10
      ->=09gLxyw kqnOA CsSXr BkuqB frGvV eTIlx FRYQT dLkyi lXdZh BbKRs=20
      ->=09BbKRs BkuqB CsSXr FRYQT dLkyi eTIlx frGvV gLxyw kqnOA lXdZh=20
      =09Location of dLkyi =3D 4

      The first line of interest shows the unsorted case, the second the
      sorted. Indeed, all uppercase characters are sorted first, which
      seems a strange thing. Is this what is intended? If no, there must
      be a bug somewhere.
      =A0=A0=A0
      (Review ID: 20687)
      ======================================================================

            jjb Josh Bloch (Inactive)
            ccresswesunw Claudette Cresswell (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: