Sorting in Java – Letters First

In my last post there was some Java code for bundling up PDF files. In this post, it’s back!

This time with the focus being on the comparison method of the filenames. The problem with the default sort is that a number will always be ranked higher than a letter.

The files I’m trying to join are named hierarchically, like this:

  • Page 0
    • Page 0.0
      • Page 0.0.Llama
    • Page 0.1
      • Page 0.1.Snake
    • Page 0.2
      • Page 0.2.Fish
  • Page 1
    • Page 1.0
      • Page 1.0.Carrot
      • Page 1.0.Parsnip
    • Page 1.1
      • Page 1.1.0
        • Page 1.1.0.Honeydew
        • Page 1.1.0.Watermelon
      • Page 1.1.1
        • Page 1.1.1.Bacon
        • Page 1.1.1.Sausage
      • Page 1.1.Lemon
    • Page 1.2
      • Page 1.2.Lamp
  • Page 2
    • Page 2.0
      • Page 2.0.Golf
      • Page 2.0.Rugby
      • Page 2.0.Skiing

There are title pages for categories and subcategories, and information pages for items within those categories. The problem with standard naming and the naming of these files can be demonstrated by the position of the highlighted item ‘lemon’. It is a direct descendant of Page 1.1. Yet It ends up positioned behind the subcategories because it ceases to have a numeric name.

This is the updated Merge.java file that forces letters to come before numbers, yet still keep them sorted correctly within themselves.

which sorts such that I get a list like this:

  • Page 0
    • Page 0.0
      • Page 0.0.Llama
    • Page 0.1
      • Page 0.1.Snake
    • Page 0.2
      • Page 0.2.Fish
  • Page 1
    • Page 1.0
      • Page 1.0.Carrot
      • Page 1.0.Parsnip
    • Page 1.1
      • Page 1.1.Lemon
      • Page 1.1.0
        • Page 1.1.0.Honeydew
        • Page 1.1.0.Watermelon
      • Page 1.1.1
        • Page 1.1.1.Bacon
        • Page 1.1.1.Sausage
    • Page 1.2
      • Page 1.2.Lamp
  • Page 2
    • Page 2.0
      • Page 2.0.Golf
      • Page 2.0.Rugby
      • Page 2.0.Skiing

Leave a Reply