-7

Hi so for example i have :

string1[0] ="Banana";
string1[1] ="Apple";
string1[2] ="Pineapple";
string1[3] ="Mandarin";

I want to sort this alphabetically using comparable and compareTo() method. So the result would be:

String1[0]="Apple";
String1[1]="Banana";
String1[2]="Mandarin";
String1[3]="Pineapple";

Could you just show me the skeleton of the code to do it plz ?

And is there a better way to sort it ?

3
  • Please try out first . Commented Apr 9, 2013 at 6:50
  • possible duplicate of Selection Sorting String Arrays (Java) Commented Apr 9, 2013 at 6:50
  • Do you know where to use compareTo()? Commented Apr 9, 2013 at 6:50

2 Answers 2

1

Just use Arrays.sort():

Arrays.sort(string1);

This will reorder the strings lexicographically.

Sign up to request clarification or add additional context in comments.

Comments

0

you can use Arrays.sort(string1);

see here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.