Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Name
{
    class LCS
    {
        int n;
        static void Main()
        {
            LCS obj = new LCS();
            obj.arrays();
            Console.ReadLine();
        }
        public void arrays()
        {
            Console.WriteLine("Enter how many strings you want to enter:-");
            n = Int32.Parse(Console.ReadLine());
            List<string> sampleList = new List<string>();
            Console.WriteLine("Enter  " + n + " strings");
            for (int i = 0; i < n; i++)
            {
                sampleList.Add(Console.ReadLine());
            }     
    }
}
Posted
Updated 5-Sep-19 21:09pm
v2
Comments
Tomas Takac 3-Nov-15 4:29am    
There are several ways, what did you try?

You can use LINQ for that:
C#
sampleList = sampleList.OrderBy(s => s.Length).ToList();

https://msdn.microsoft.com/en-us/library/vstudio/bb534966%28v=vs.100%29.aspx[^]
 
Share this answer
 
Comments
Leo Chapiro 3-Nov-15 4:35am    
n1 (Nice one), +5 :)
Thomas Daniels 3-Nov-15 4:36am    
Thank you!
Like I said there are several ways to do this. And of course ProgramFOX's solution with LINQ is correct. So here is an alternative using List.Sort method[^]:
C#
sampleList.Sort((x,y) => x.Length - y.Length);

The benefit is you are not creating a new instance of List.
 
Share this answer
 
Comments
BillWoodruff 3-Nov-15 11:49am    
+5
George Swan 6-Sep-19 3:34am    
Excellent answer. I was so obsessed by Linq that I didn't think of your solution. And, come to think of it, there is also
sampleList.Sort((x, y) => x.Length.CompareTo(y.Length));
using System;
using static System.Console;
using static System.Convert;

namespace Program
{
    class arrexp
    {
        static void Main(string[] args)
        {
            int size;
            WriteLine("Enter size");
            size = ToInt32(ReadLine());

      
            string[] str = new string[size];
     
            int[] b = new int[size];
            WriteLine("Enter the words");
            for (int i = 0; i < size; i++)
            {
                str[i] = ReadLine();
            }
            for (int i = 0; i < size; i++)
            {
                for (int k = i + 1; k < size; k++)
                {
                    if(str[i].Length>str[k].Length)
                    {
                        string str1 = " ";
                        str1 = str[i];
                        str[i] = str[k];
                        str[k] = str1;
                    }
                    

                }
            }
            foreach(string x in str)
            {
                WriteLine(x);
            }
        }
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900