Sorting the array of strings on their lengths in Java , Using Maps, ascending order.... I wrote the java method to answer this question :
Write a Program to sort String on their length in Java? Your method should accept  an array of String and return a sorted array based upon the length of String

So my questions:
1. Is my method efficient enough?
2. Can it be improved somehow?

 private static String[] sort(String [] string) {
    /*Local variables*/
    Map&lt;String, Integer&gt; map=new LinkedHashMap&lt;String, Integer&gt;();
    Map&lt;String, Integer&gt; mapCopy=new LinkedHashMap&lt;String, Integer&gt;();
    int [] lengthsArray=new int[string.length];
    String [] sortedStrings=new String[string.length];
    int counter1=0;
    int counter2=0;

    /* Store all the pairs &lt;key,value&gt;
     * i.e &lt;string[i],string[i].length&gt;
     */
    for(String s:string){
        map.put(s, s.length());
        lengthsArray[counter1]=s.length();//store all the lengths
        counter1++;

    }
    mapCopy=new LinkedHashMap&lt;String, Integer&gt;(map);//make a copy of map
    Arrays.sort(lengthsArray);//sort the array of lengths

    /*
     * Sort array according to the array of lengths
     * by finding the matching value from the map 
     * then add it to the final string array,and then remove it from the map 
     */
    for(int item:lengthsArray){

        for(Map.Entry&lt;String, Integer&gt; e:map.entrySet()){
            if(item==e.getValue()){
                sortedStrings[counter2]=e.getKey();
                counter2++;
                map.remove(e.getKey());
                break;
            }   
        }
    }

    map=mapCopy;
    System.out.println(map);//print map
    return sortedStrings;
}
I think you can use the Arrays.sort with Comparator to make this simple. just Override the comparator to compare the length of the array like this

public static String[] sortStrArray(String[] array){
   //sort arrays by length before returning
    Arrays.sort(array, new Comparator&lt;String&gt;() {
        @Override
        public int compare(String a, String b) {
            return Integer.compare(a.length(),b.length());//specifying compare type that is compare with length
        }
    });
     return array;
}


///Main Method here

String[] str = {"James","Ana","Michael","George","rose"};
    System.out.println(Arrays.toString(sortStrArray(str)));


Output:


  [Ana, rose, James, George, Michael]


with Java 8

 public static String[] sortStrWithLam(String[] array){
    //sort arrays by length before returning
    Arrays.sort(array,(a,b) -&gt; Integer.compare(a.length(),b.length()));
    return array;
}