Run Code
|
API
|
Code Wall
|
Users
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Rank
//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 static class Program { public static void Main(string[] args) { string[] myNames = new string[] { "Bob", "Mark", "John", "Jim", "Lisa", "Dave" }; var query = myNames.Rank(s => s.Length, (s, r) => new { s, r }); foreach (var x in query) { Console.WriteLine("{0} {1}", x.r, x.s); } } public static IEnumerable<U> Rank<T, TKey, U> ( this IEnumerable<T> source, Func<T, TKey> keySelector, Func<T, int, U> selector ) { if (!source.Any()) { yield break; } int itemCount = 0; T[] ordered = source.OrderBy(keySelector).ToArray(); TKey previous = keySelector(ordered[0]); int rank = 1; foreach (T t in ordered) { itemCount += 1; TKey current = keySelector(t); if (!current.Equals(previous)) { rank = itemCount; } yield return selector(t, rank); previous = current; } } } }
run
|
edit
|
history
|
help
0
Please
log in
to post a comment.
Math 10.95 fracOp (mult div only)
Roman Numbers - Sorting
Prime number
Fórum ➡ GroupJoin'ing Books and Orders ( with Zip'ped output ) ♦
2.2 gz
Compare 2 int
Code which will convert Word into its Binary Format
Intuit // C# // listing_4.10 (for..)
Delegate-ModifyObject
054325462
Please log in to post a comment.