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

Arrays.sort places the first entry in the array last when it should be first

    Details

      Description

      ADDITIONAL SYSTEM INFORMATION :
      SE runtime build is 1.8.0.201-b09. Hotspot 64-bit build 25.201.b09 mixed mode.

      A DESCRIPTION OF THE PROBLEM :
      Using a comparator with arrays sort the value "aah " comes after "aalii " and after "zythum ". All other entries are sorted correctly. So the sort is correct from aalii to zythem, then aah is the final element in the sorted array. The hex values in the input file for aah are: 61 61 68 20 and directly follow the magic number EF BB BF. Using compareToIgnoreCase the comparison returns abacist to aah -65182; aah to zythum 65157; abacist to aalii 1. The array size is 306,962 elements.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Create a 307,000 list of words beginning with aah and ending with zythum and sort it using a class comparator.


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      All elements sorted alphabetically in ascending order.
      ACTUAL -
      Sort is perfect except that what should be the first entry ends up last.

      ---------- BEGIN SOURCE ----------
      The input file is probably more useful than code.
      The invocation is after converting an ArrayList to an array:
      DictEntry dea[] = new DictEntry[dictionary.size()];
      dea = dictionary.toArray(dea);
      Arrays.sort(dea, new DictSortEntries());
      My DictEntry class has three elements: word, part-of-speech and definition.
      public final class DictEntry {
      String word, pos, definition;
      }
      And the comparator:
      public class DictSortEntries implements Comparator<DictEntry> {
      public int compare(DictEntry a, DictEntry b) {
      return a.word.compareToIgnoreCase(b.word);
      }
      }


      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      No good one. Implement my own sort.

      FREQUENCY : always


        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                psonal Pallavi Sonal (Inactive)
                Reporter:
                webbuggrp Webbug Group
              • Votes:
                0 Vote for this issue
                Watchers:
                4 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: