1

This might look dulicate question and is a bit relevant to

Alphabetical sorting in treeset not working

I have a Treeset containing

TreeSet<String> ts=new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);

        ts.add("D1");
        ts.add("D9");
        ts.add("D5");
        ts.add("D3");
        ts.add("D8");

which gives me,

Tree set :: [D1, D3, D5, D8, D9]

but if i add double digits to my "D"

    ts.add("D1");
    ts.add("D9");
    ts.add("D5");
    ts.add("D3");
    ts.add("D8");
    ts.add("D11");
    ts.add("D18");
    ts.add("D17");
    ts.add("D13");

i get,

Tree set :: [D1, D11, D13, D17, D18, D3, D5, D8, D9]

which aint correct....Please help!

Community
  • 1
  • 1
  • It seems correct to me. First the first character is taken into account for ordering, then the second, ... – K.C. Jan 28 '14 at 06:41

1 Answers1

0

Sorting is performed alphabetically - all 1 (ones) are coming before 3, then comes 5, 8, 9.

If you want to have numeric sorting you need to implement your own comparator.

Petro Semeniuk
  • 6,970
  • 10
  • 42
  • 65