var non_community1 =new List<string>{ "red-11","red-10","red-9","orange-11","green-11","green-7","green-9", "green-9" ,"orange-9","green-11"};
non_community1 = non_community1.OrderByDescending(s => int.Parse(Regex.Match(s, @"\d ").Value)).ToList();
for (int i = non_community1.Count - 1; i > 0; i--)
{
if ((non_community1[i] == non_community1[i - 1]))
{
non_community1.RemoveAt(i);
}
}
this code give me that to sort
this is the list i want to produce
I'm a little stuck at this part, how do I get out of it?
I want to write the same at least 3 numbers in different color groups in sequence and I want to add "null" between the numbers that write 3 or more consecutive numbers.
CodePudding user response:
ok so (after you removed the duplicates) you would need to use GroupBy
to aggregate the items with the same integer and sort them according to their occurence:
var groupedResult = non_community1
.GroupBy( s => int.Parse(Regex.Match(s, @"\d ").Value))
.OrderByDescending(k => k.Count()).ToList();
Now you need to go through each group and collect them in the final list.
and I want to add "null" between the numbers that write 3 or more consecutive numbers.
For this you can check how many items are in each group if you have more than 2 items then add the null
item.
List<string> finalList = new List<string>();
for ( int i = 0; i < groupedResult.Count; i )
{
finalList.AddRange(groupedResult[i]);
if(groupedResult[i].Count() > 2)
{
finalList.Add(null);
}
}
Console.WriteLine(string.Join(Environment.NewLine, finalList));
and you should have your desired result