-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 ?

Shreyos Adikari
  • 12,348
  • 19
  • 73
  • 82
Tommy
  • 427
  • 2
  • 5
  • 14

2 Answers2

1

Just use Arrays.sort():

Arrays.sort(string1);

This will reorder the strings lexicographically.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
0

you can use Arrays.sort(string1);

see here

PSR
  • 39,804
  • 41
  • 111
  • 151