How to order an array of strings first by string length and then by alphabet with Linq in C#

1 Answer

0 votes
using System;
using System.Linq;

class Program
{
    static void Main() {
        string[] arr = { "c#", "c", "rust", "dart", "c++", "php", "python", "java" };

        var result = (from c in arr
                        orderby c.Length
                        select c)
                        .ThenBy(c => c);

        foreach (string s in result)
                Console.WriteLine(s);
    }
}




/*
run:

c
c#
c++
php
dart
java
rust
python

*/

 



answered Jan 2, 2023 by avibootz
...