Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

List sort according to desired condition

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 code give me that to sort

this is the list i want to produce

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading