HoneyWine
HoneyWine

Reputation: 187

How to sort strings containing multiple periods/dots in Java?

I want to sort a few strings:

 RANDOM.XY
 AAA.BBB.CC
 AAA.BBB
 RANDOM.XY.Z
 AAA.BBB.CC.D

The sorted order should be:

 AAA.BBB
 AAA.BBB.CC
 AAA.BBB.CC.D
 RANDOM.XY
 RANDOM.XY.Z

I put them in an ArrayList list, and used a built-in sort method:

 Collections.sort(list);

However, what I get from the sorting is:

 AAA.BBB.CC.D
 AAA.BBB.CC
 AAA.BBB
 RANDOM.XY.Z
 RANDOM.XY

How should I go about this?

Upvotes: 0

Views: 563

Answers (1)

Peter Zhu
Peter Zhu

Reputation: 461

You can create your custom class that contains the original string and a string that doesn't contain the dots. You then can implement the Comparator interface and compare the strings that doesn't contain the dots.

Upvotes: 2

Related Questions