How to sort an array by string length in ascending order with C#

1 Answer

0 votes
using System;
using System.Linq;
using System.Collections.Generic;

public class Program
{
    public static void Main()
    {
        string[] arr = new[] { "c#", "c", "c++", "vb.net", "javascript", "java", "python", "node.js" };

        IEnumerable<string> query = arr.OrderBy(s => s.Length).ThenBy(s => s);

        foreach (string str in query)
            Console.WriteLine(str);
    }
}





/*
run:

c
c#
c++
java
python
vb.net
node.js
javascript

*/

 



answered Aug 6, 2022 by avibootz

Related questions

...