Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Sort string collection by length
//Rextester.Program.Main is the entry point for your code. Don't change it. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Program { public static void Main(string[] args) { var stringCollection = new string[]{"asdf", "sdf", "asdfdfsf", "sdf", "sdfdf", "sfsasdfasfasf", "asfd", "dfd", "sdfasdfsfa", "sdfs", "fdsdf", "d", }; //Your code goes here foreach(var str in SortByLength2(stringCollection)){ Console.WriteLine(str); } } private static string[] SortByLength(string[] coll){ //Array.Sort(coll, CompareStringByLength); Array.Sort(coll, new ComparerByLength()); //return coll.Sort<string>(CompareStringByLength); return coll; } private static int CompareStringByLength(string str1, string str2){ return str1.Length - str2.Length; } private static string[] SortByLength2(string[] coll){ return coll.OrderBy(s => s.Length).ToArray(); } } public class ComparerByLength : IComparer<string>{ public int Compare(string str1, string str2){ return str1.Length - str2.Length; } } }
run
|
edit
|
history
|
help
0
sorted array
HashItUp
test
NumberToWords C# Structural
Covariance & Contravariance
Only certain spheres.
sdfghyjtgfredx
Main4-2
Prime no for specified max no by user(Using While Loop)
math 5.74