0

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.

   using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Collections;

namespace Laboratorinis_P3
{
    public partial class Form1 : Form
    {
        List<Museum> firstList;
        List<Museum> newList;
        List<Museum> twoList;
        List<Museum> TTList;
        public Form1()
        {
            InitializeComponent();
        }
        private void formListBySelectedCityToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string city = Convert.ToString(Cities.SelectedItem);
            LinqForming(city);
           
        }
        private void findTwoMuseumsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FindTwoMuseums(firstList);
        }
        private void readFileAndPrintItToolStripMenuItem_Click(object sender, EventArgs e)
        {
          
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
            openFileDialog1.Title = "Pasirinkite duomenų failą";
            DialogResult result = openFileDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                string path = openFileDialog1.FileName;
                firstList = ReadFile(path,firstList);
                PrintList(firstList,"Read file");
                AddCitiesToComboBox(firstList);
            }
 
        }
       
        static List<Museum> ReadFile(string fv,List<Museum> firstList)
        {
            
            firstList = new List<Museum>();
            using (StreamReader reader = new StreamReader(fv))
            {

                string line;
                
                while ((line = reader.ReadLine()) != null)
                {
                    ArrayList days = new ArrayList();
                    string[] parts = line.Split(";");
                    string name = parts[0];
                    string city = parts[1];
                    string type = parts[2];

                    string[] day = parts[3].Trim().Split(new[] {' '});
                    days.Clear();
                    foreach (string lines in day)
                    {
                        
                        int dayss = int.Parse(lines);
                        days.Add(dayss);
                    }

                    double adult = double.Parse(parts[4]);
                    double kid = double.Parse(parts[5]);
                    string hasguide = parts[6];
                    Museums museum = new Museums(name, city, type, days, adult, kid, hasguide);
                    firstList.Add(museum);

                }
            }
            return firstList;
        }
        private void LinqForming(string city)
        {
            newList = firstList                     
                      .Where(x => x.City == city)
                      .ToList();
        }
       
        private void AddCitiesToComboBox(List<Museum> list)
        {
            for (int i = 0; i < list.Count; i++)
            {
                if (!Cities.Items.Contains(list[i].City))
                {
                    Cities.Items.Add(list[i].City);
                }
            }
        }
        private void FindTwoMuseums(List<Museum> museum)
        {

           
        
        }
        private void PrintList(List<Museum> museum, string info)
        {
            finalResults.Text += info + "\n";
            string top =
                "|------|---------|---------|-------------|--------|------|-----------|\n"
               + "| Name | City    | Type    | Days        |  Adult |  Kid | Has guide |\n"
               + "|------|---------|---------|-------------|--------|------|---------- |\n";

            finalResults.Text += top;
            for (int i = 0; i < museum.Count; i++)
            {
                finalResults.Text += museum[i] + "\n";
            }
        }
    }
}

namespace Laboratorinis_P3
{
    class Museum
    {
        public string Name { get; set; }
        public string City { get; set; }
        public string MuseumTypes { get; set; }

        public ArrayList Days { get; set; }
        public double PriceForAdult { get; set; }
        public double PriceForKid { get; set; }
        public string HasGuide { get; set; }

        public Museum()
        {

        }

        public Museum(string name, string city, string type, ArrayList days,
                      double adult, double kid, string hasguide)
        {
            this.Name = name;
            this.City = city;
            this.MuseumTypes = type;
            this.Days = days;
            this.PriceForAdult = adult;
            this.PriceForKid = kid;
            this.HasGuide = hasguide;
        }

        public override string ToString()
        {
            string eilute;
            eilute = string.Format
            ("|{0,6}|{1,9}|{2,9}|{3,13}|{4,8}|{5,6}|{6,11}|", Name, City, MuseumTypes,
             String.Join(" ", Days.ToArray().Select(s => s.ToString())), PriceForAdult, PriceForKid, HasGuide);

            return eilute;
        }
    }
}


Arnas
  • 1
  • 5

3 Answers3

0

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();
}
Maxi Jabase
  • 59
  • 1
  • 2
0

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.

Curt Toppin
  • 19
  • 1
  • 5
0

If you have a list of list, use select many to flatten it

https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.selectmany?redirectedfrom=MSDN#overloads

Lee
  • 703
  • 6
  • 20