Home > Back-end >  How to write a arrayList into a array?
How to write a arrayList into a array?

Time:04-02

I am trying to get the count of an arrayList numbers which represent days(which are in a List) written into a another array so I can sort it.

Array list:

2 3 4 5 6 7

1 2 3 4

1 2 3

5 6 7

1 2 3

1 2 3 4 5 6 7

In this case I can print the count of each ArrayList, I just can't work with it. I wish to sort it so I can get two biggest numbers from it.

   private void FindTwoMuseums(List<Museum> museum)
        {


            int[] arr = { };
            int firstIndex = 0;
            int secondIndex = 0;
            for (int i = 0; i < museum.Count; i  )
            {
 
                arr  = museum[i].Days.Count;
       
            }
        }

CodePudding user response:

If you want to return the two Museums with the highest Days property, you can do:

using System.Linq;

List<Museum> FindTwoMuseums(List<Museum> list)
{
    return list.OrderByDescending(x => x.Days).Take(2).ToList();
}

CodePudding user response:

You could dump all the values into a single list using something like this:

List<int> allDays = new List<int>;
for (int i = 0; i < museum.Count; i  )
{
    foreach (int day in museum[i].Days)
    {
        allDays.Add(day);
    }
}

Then you could just sort that list and easily find your two largest ints.

  •  Tags:  
  • c#
  • Related