Quantcast
Viewing all articles
Browse latest Browse all 35

Answer by user2023861 for Sorting a list of first names from a text file

Here's a way to do it in one line of Linq code:

int score = System.IO.File.ReadAllLines(ConfigurationManager.AppSettings["LocationOfNamesFile"])    .SelectMany(s => s.Split(','))    .OrderBy(b => b)    .Select((s, i) => new {         Index = i + 1,         Score = s.Sum(ch => (int)(char.ToUpper(ch) - 'A'+ 1)) })    .Sum(s => s.Index * s.Score);

This will throw an exception if the file doesn't exist, but your original code does this as well.


Viewing all articles
Browse latest Browse all 35

Trending Articles