0

I am probably overthinking this all and I'm lost. I'm new to C# and don't know what is the best way to solve this.

string[] input = new string { "FR_Paris", "UK_London", "UK_Bristol" };

Desirable output in console is ordered by occurrence of cities in a country and cities are sorted alphabetically.

In this situation it is:

  • UK 2x Bristol, London
  • FR 1x Paris

I'm not going to lie, this is my homework. I know how to parse the input and I think for cities has to be used a collection which can be sorted but don't know which type. I'm kind of lost when it comes to nested collections.

Please give me at least a direction.

Thanks a lot!

Martin
  • 1
  • 1
  • Does this answer your question? [Sort List by occurrence of a word by LINQ C#](https://stackoverflow.com/questions/11392014/sort-list-by-occurrence-of-a-word-by-linq-c-sharp) – IndieGameDev Nov 12 '20 at 07:46
  • First of all, Counting each country, IF it's separated by '_' u can split string get the ASCII code of it and store it an array and then get the count from that array(or you can use Regex, but i can it a bit advanced for homeworkd), and for the sort, u can use Linq built-in sort function – styx Nov 12 '20 at 07:47
  • 2
    What are the string values of `FR_Paris`, `UK_London`, and `UK_Bristol`? – John Wu Nov 12 '20 at 07:47
  • The data structure you are looking for (probably) is a [`Lookup`](https://stackoverflow.com/questions/1403493/what-is-the-point-of-lookuptkey-telement), which you can create from an array using [`ToLookup()`](https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.tolookup?view=net-5.0). – John Wu Nov 12 '20 at 07:49
  • @styx , good point, sorry for that. I skipped " " The post is edited – Martin Nov 12 '20 at 08:01

3 Answers3

0

Try This

// Init Array
string[] input = new string[] { "FR_Paris", "UK_London", "UK_Bristol" };

//Get All Codes
List<string> Codes = new List<string>();
foreach (var CityName in input)
{
   var Name = CityName.Split('_')[0]; // Get Name After _ Paris
   if (!Codes.Contains(Name))
      Codes.Add(va);
}

// Print
foreach (var Code in Codes.OrderBy(x => x))
{
   var AllNames = input.Where(x => x.StartsWith(Code + "_")).Select(x => x.Split('_')[1]);

   Console.WriteLine(Code + " " + xd.Count() + "x " + string.Join(",", AllNames.OrderBy(x => x)));
}
0

You can try with linq

var input = new List<string> { "FR_Paris", "UK_London", "UK_Bristol" };

var result = input.Select(x => new { Country = x.Split("_")[0], City = x.Split("_")[1] })
                  .GroupBy(x => x.Country)
                  .Select(x => $"{x.Key} {x.Count()}x {String.Join(", ", x.OrderBy(x => x.City).Select(x => x.City))} ");
foreach (var item in result)
{
    Console.WriteLine(item);
}

OUTPUT

FR 1x Paris
UK 2x Bristol, London
Krishna Varma
  • 4,238
  • 2
  • 10
  • 25
0

Using LINQ:

var grouped = input
    .Select(x => x.Split('_')) // Split all strings into array[2]
    .GroupBy(x => x[0]) // Group by country
    .OrderByDecending(x => x.Count()); // Order by number of cities
Johnathan Barclay
  • 18,599
  • 1
  • 22
  • 35