Skip to content
Advertisement

Java how to sort from Text File

CH0001 , John
CH0002 , Alex

Above is the coach.txt which contains id and name. I’m still new to Java. How do I sort it according to the name.

CH0002 , Alex
CH0001 , John

My expected output would be like this. Kindly appreciate for you guys help!

Advertisement

Answer

There are many alternatives.

The main issue here should be comparing the lines not according to the alphanumeric value of the whole line, but separating the lines into columns, and using the second column’s alphanumeric value as the comparator.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement