How to find the Prefix of one string into another

Mam Can u please help me how to find prefix in a particular string with code implementation.

You are given three strings:
bat
apple
batman

Now you have to arrange it right? so you will see a is coming forward so yes

apple came

after that(you have to handle this with a comparator function)

bat is shorter then batman 
so it should have come before batman 
but here is the twist

When you get two strings such that one string can come under another string like bat came in batman. In this case we will print batman first and then bat.
So expected output is:

apple
batman
bat