user3243246
user3243246

Reputation: 11

Treeset Ordering on string Not performed

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!

Upvotes: 1

Views: 57

Answers (1)

Petro Semeniuk
Petro Semeniuk

Reputation: 7038

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.

Upvotes: 0

Related Questions