Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

sort array based on string length java

import java.util.*;
class Main
{
    public static void main(String[] args) 
    {
        ArrayList<String> arr = new ArrayList<>();
        Scanner sc = new Scanner(System.in);
        
        int n = sc.nextInt();
        sc.nextLine();
        
        for(int i=0; i<n; i++){
            arr.add(sc.nextLine());
        }
        
        for(int i=0;i<n;i++){
            for(int j=i+1;j<n;j++){
                if((arr.get(i)).length() > (arr.get(j)).length()){
                    Collections.swap(arr,i,j);
                }
            }
        }
        
        System.out.println(arr);
        
    }
}
 
PREVIOUS NEXT
Tagged: #sort #array #based #string #length #java
ADD COMMENT
Topic
Name
4+4 =