Sort an array of strings according to string lengths | C # program to sort an Array of Strings according to their lengths ; Function to Sort the array of string according to lengths . This function implements Insertion Sort . ; Insert s [ j ] at its correct position ; Function to print the sorted array of string ; Driver function ; Function to perform sorting ; Calling the function to print result
using System ; public class solution { static void sort ( String [ ] s , int n ) { for ( int i = 1 ; i < n ; i ++ ) { String temp = s [ i ] ; int j = i - 1 ; while ( j >= 0 && temp . Length < s [ j ] . Length ) { s [ j + 1 ] = s [ j ] ; j -- ; } s [ j + 1 ] = temp ; } } static void printArraystring ( String [ ] str , int n ) { for ( int i = 0 ; i < n ; i ++ ) Console . Write ( str [ i ] + " ▁ " ) ; } public static void Main ( ) { String [ ] arr = { " GeeksforGeeks " , " I " , " from " , " am " } ; int n = arr . Length ; sort ( arr , n ) ; printArraystring ( arr , n ) ; } }