I have a String Vector that contains data like this :
5:34, 5:38, 17:21, 22:11, …
If i try to merge this using Collections.sort( … ); it will appear like this :
17:21, 22:11, 5:34, 5:38
Actually i want it to appear like this :
5:34, 5:38, 17:21, 22:11
So i want to sort the elements according to the number before the colon “:” then if some elements have the same number before “:” then sort them according to the number after the “:”.
What is the simplest way to do this ?
You could either create a custom
Comparatorto split theStringand parse it into two ints, or create a bespoke class to represent eachStringand store that in theCollectioninstead. I favour the latter approach as you only incur the overhead of splitting / parsing the String once; e.g.Then it’s simply a case of storing some
Dataobjects in yourCollection; e.g.One more thing – You mention you’re using a
Vector. You should try to avoid usingVector/Hashtableas these have been superseded byList/Map, which were introduced as part of the Collections Framework in JDK 1.2.