static void Main()
        {
            Random rand = new Random();
            Student[] students = new Student[10];
            string[] firstNames = new string[] { "Niki", "Doncho", "Pesho", "Maria", "Stamat", "Ivan", "Ivo", "Gosho", "Evlogi", "Svetlin" };
            string[] lastNames = new string[] { "Kostov", "Minkov", "Peshev", "Ivanov", "Stamatov", "Popov", "Kenov", "Goshev", "Hristov", "Nakov" };

            for (int i = 0; i < students.Length; i++)
            {
                string firstName = firstNames[rand.Next(0, 10)];
                string lastName = lastNames[rand.Next(0, 10)];
                uint grade = (uint)rand.Next(1, 5);
                Student student = new Student(firstName, lastName, grade);
                students[i] = student;
            }

            Console.WriteLine("STUDENTS");
            var studentsByGrade = students.OrderBy(st => st.Grade).ToList();
            foreach (var student in studentsByGrade)
            {
                Console.WriteLine("{0} {1}, grade --> {2}", student.FirstName, student.LastName, student.Grade);
            }

            Worker[] workers = new Worker[10];
            for (int i = 0; i < workers.Length; i++)
            {
                string firstName = firstNames[rand.Next(0, 10)];
                string lastName = lastNames[rand.Next(0, 10)];
                decimal weekSalary = (decimal)rand.Next(100, 200);
                Worker worker = new Worker(firstName, lastName, weekSalary, 8);
                workers[i] = worker;
            }

            Console.WriteLine("WORKERS");
            var workersByHourMoney = workers.OrderByDescending(w => w.MoneyPerHour()).ToList();
            foreach (var worker in workersByHourMoney)
            {
                Console.WriteLine("{0} {1} --> {2} money per hour", worker.FirstName, worker.LastName, worker.MoneyPerHour());
            }

            var mergedList = students.Union<Human>(workers).OrderBy(h => h.FirstName).ThenBy(h => h.LastName);
            Console.WriteLine("HUMANS LIST");
            foreach (var human in mergedList)
            {
                string type = human.GetType().Name;
                Console.WriteLine("{0} {1} type --> {2}", human.FirstName, human.LastName, type);
            }
        }
        static void Main()
        {
            List<Student> students = new List<Student>();

            Student student = new Student("Nikola", "Ivanov", 1);
            students.Add(student);
            student = new Student("Zdravka", "Ivanova", 2);
            students.Add(student);
            student = new Student("Dimana", "Todorova", 1);
            students.Add(student);
            student = new Student("Stanimir", "Krasenov", 3);
            students.Add(student);
            student = new Student("Stojan", "Nikolov", 5);
            students.Add(student);
            student = new Student("Zornica", "Petrova", 1);
            students.Add(student);
            student = new Student("Aleksandar", "Gorspodinov", 2);
            students.Add(student);
            student = new Student("Nikolaj", "Ivanov", 3);
            students.Add(student);
            student = new Student("Kiril", "Kirilov", 2);
            students.Add(student);
            student = new Student("Nikola", "Petrov", 5);
            students.Add(student);

            //sort them by grade in ascending order (use LINQ or  OrderBy()  extension method).
            var studentsSortedByGrade = students.OrderBy(stud => stud.Grade);

            Console.WriteLine("Students sorted by grade:");

            foreach (var item in studentsSortedByGrade)
            {
                Console.WriteLine(item);
            }

            Console.WriteLine();

            List<Worker> workers = new List<Worker>();

            Worker worker = new Worker("Petar", "Ivanov", 600m, 8, 5);
            workers.Add(worker);
            worker = new Worker("Petarana", "Nikolova", 300m, 8, 5);
            workers.Add(worker);
            worker = new Worker("Stojan", "Angelov", 400m, 8, 5);
            workers.Add(worker);
            worker = new Worker("Momchil", "Stojanov", 700m, 8, 5);
            workers.Add(worker);
            worker = new Worker("Dona", "Stoeva", 200m, 8, 5);
            workers.Add(worker);
            worker = new Worker("Todor", "Koev", 500m, 24, 2);
            workers.Add(worker);
            worker = new Worker("Kosta", "Ivanov", 700m, 8, 5);
            workers.Add(worker);
            worker = new Worker("Dimitar", "Angelov", 400m, 8, 5);
            workers.Add(worker);
            worker = new Worker("Angel", "Ivanov", 300m, 8, 5);
            workers.Add(worker);
            worker = new Worker("Petar", "Koev", 200.20m, 12, 2);
            workers.Add(worker);

            //sort them by money per hour in descending order.

            var workersSortedByMoneyPerHour = workers
                .Select(wr => new { Name = string.Format("{0} {1}", wr.FirstName, wr.LastName), MoneyPerHour = wr.GetMoneyPerHour() })
                .OrderByDescending(newwr => newwr.MoneyPerHour);

            Console.WriteLine("Workers sorted by money per hour");

            foreach (var item in workersSortedByMoneyPerHour)
            {
                Console.WriteLine(item);
            }

            Console.WriteLine();

            //Merge the lists and sort them by first name and last name.
            var humansList = students.Select(st => (Human)st)
                .Concat(workers.Select(wr => (Human)wr))
                .OrderBy(h => h.FirstName)
                .ThenBy(h => h.LastName);

            Console.WriteLine("Humans sorted by first name and last name");

            foreach (var item in humansList)
            {
                Console.WriteLine(item);
            }

            Console.WriteLine();
        }
Esempio n. 3
0
        static void Main(string[] args)
        {
            //Initialize some students
            var stud1 = new Student("Antoan", "Elenkov", 9);
            var stud2 = new Student("Georgi", "Ivanov", 8);
            var stud3 = new Student("PEtar", "Petrov", 12);
            var stud4 = new Student("Angel", "Angelov", 10);
            var stud5 = new Student("Ivan", "Nasran", 7);
            var stud6 = new Student("Dqdo", "Mraz", 9);
            var stud7 = new Student("Super", "Man", 8);
            var stud8 = new Student("Jelezniqt", "Chovek", 12);
            var stud9 = new Student("Telerik", "Ninja", 10);
            var stud10 = new Student("Daffy", "Duck", 7);

            //Initialize some workers
            var worker1 = new Worker("Gosho", "Zidarov", 1100, 40);
            var worker2 = new Worker("Petio", "Iordanov", 1200, 40);
            var worker3 = new Worker("Dqdo", "Koleda", 1500, 30);
            var worker4 = new Worker("Bai", "Ivan", 1500, 40);
            var worker5 = new Worker("Bai", "Kolio", 2500, 20);
            var worker6 = new Worker("Baba", "Koleda", 3000, 60);
            var worker7 = new Worker("Zidar", "Mazachov", 1020, 50);
            var worker8 = new Worker("Maistor", "Pesho", 1200, 25);
            var worker9 = new Worker("Master", "Petrov", 1020, 35.5f);
            var worker10 = new Worker("Number", "One", 1500, 28);

            //List of students
            var listOfStudents = new List<Student>();
            listOfStudents.Add(stud1);
            listOfStudents.Add(stud2);
            listOfStudents.Add(stud3);
            listOfStudents.Add(stud4);
            listOfStudents.Add(stud5);
            listOfStudents.Add(stud6);
            listOfStudents.Add(stud7);
            listOfStudents.Add(stud8);
            listOfStudents.Add(stud9);
            listOfStudents.Add(stud10);

            //The students ordered by grade in ascending order
            var orderByGrade = listOfStudents.OrderBy(x => x.Grade)
                .Select(x=>new{x.FirstName,x.LastName,x.Grade});
            Console.WriteLine("The students ordered by grade in ascending order:");
            Console.WriteLine(String.Join(Environment.NewLine,orderByGrade));
            Console.WriteLine();

            //List of workers
            var listOfWorkers= new List<Worker>();
            listOfWorkers.Add(worker1);
            listOfWorkers.Add(worker2);
            listOfWorkers.Add(worker3);
            listOfWorkers.Add(worker4);
            listOfWorkers.Add(worker5);
            listOfWorkers.Add(worker6);
            listOfWorkers.Add(worker7);
            listOfWorkers.Add(worker8);
            listOfWorkers.Add(worker9);
            listOfWorkers.Add(worker10);

            //The workers ordered by wage perh hour in descending order
            var orderByWage = listOfWorkers.OrderByDescending(x => x.CalculateMoneyPerHour())
                .Select(x => new { x.FirstName, x.LastName, wage = x.CalculateMoneyPerHour() });
            Console.WriteLine("The workers ordered by wage perh hour in descending order:");
            Console.WriteLine(String.Join(Environment.NewLine, orderByWage));
            Console.WriteLine();

            //The workers and students ordered by first and last name
            var orderAllByFirstAndLastName = listOfWorkers.Cast<Human>()
                .Union(listOfStudents.Cast<Human>())
                .OrderBy(x=>x.LastName)
                .OrderBy(x=>x.FirstName)
                .Select(x=>new{x.FirstName,x.LastName});
            Console.WriteLine("The workers and students ordered by first and last name:");
            Console.WriteLine(String.Join(Environment.NewLine, orderAllByFirstAndLastName));
        }
Esempio n. 4
0
        static void Main()
        {
            Student[] students = new Student[]
            {
                new Student("Ivan","Petrov",Grade.Apprentice),
                new Student("Penka","Tvurdata",Grade.Junior),
                new Student("Mikta","Lesnata",Grade.Collage_boy),
                new Student("Kraso","Burziq",Grade.Bachelor),
                new Student("Bobi","Skiorche",Grade.MBA),
                new Student("Martin","Petrov",Grade.Junior),
                new Student("Lazar","Petrovich",Grade.High_School_boy),
                new Student("Stefan","Georgiev",Grade.Master),
                new Student("Ivo","Krastev",Grade.Apprentice),
                new Student("Krasi","Kciq",Grade.Master),

            };

            IEnumerable<Student> query = students.OrderBy(student => student.LastName);

            foreach (Student student in query)
            {
                Console.WriteLine("{0} {1}",student.FirstName,student.LastName);
            }

            Console.WriteLine("Workers by wage:");

            IWorker[] workers = new IWorker[]
            {
                new Worker("Mitko", "Gotiniq",1000,8),
                new Worker("Galq", "Krasavicata",600,8.5m),
                new Worker("Ivancho", "Georgiev",2340,12),
                new Worker("Ivanka", "Marinova",2010,10.5m),
                new Worker("Marin", "Paraskevov",340,4),
                new Worker("Iliq", "Rijiq",2550,2),
                new Worker("Joro", "Georgiev",600,8),
                new Worker("Liq", "Ninjata",400,8),
                new Worker("Pavlin", "Turnovski",3128,8),
                new Worker("Reni", "Milata",1240,9),
                new Worker("Georgi", "Shalamanoff",880,7),
            };


            SortedDictionary<decimal, Worker> queryTwo = new SortedDictionary<decimal, Worker>();
            foreach (Worker item in workers)
            {
                queryTwo.Add(item.MoneyPerHour(), item);
            }

            foreach (KeyValuePair<decimal, Worker> kvp in queryTwo)
            {
                Console.WriteLine("{0:F2} money per hour is the wage of: {1}",kvp.Key,kvp.Value.LastName);
            }

            Console.WriteLine("All initialized humans");

            SortedDictionary<string, string> queryThree = new SortedDictionary<string, string>();
            foreach (Student item in students)
            {
                queryThree.Add(item.FirstName, item.LastName);
            }
            foreach (Worker item in workers)
            {
                queryThree.Add(item.FirstName, item.LastName);
            }

            foreach (var member in queryThree)
            {
                Console.WriteLine("{0} {1}",member.Key,member.Value);
            }
        }
Esempio n. 5
0
        static void Main()
        {
            Student[] students = new Student[]
            {
                new Student("Ivan", "Petrov", 5.50),
                new Student("Alex", "Simeonov", 2.75),
                new Student("Iskra", "Radeva", 5.84),
                new Student("Dani", "Kostov", 6.00),
                new Student("Dimo", "Dimov", 2.25),
                new Student("Silviya", "Eneva", 4.33),
                new Student("Oleg", "Harizanov", 3.44),
                new Student("Nikolay", "Tzonkov", 2.87),
                new Student("Viki", "Nikolova", 4.92),
                new Student("Yanko", "Yankov", 5.12)
            };

            Worker[] workers = new Worker[]
               {
               new Worker("Bistra", "Pavlova", 258.25m, 10),
               new Worker("Veni", "Tasheva", 1020.80m, 8),
               new Worker("Daniel", "Elenov", 140.65m, 6),
               new Worker("Angel", "Angelov", 940.48m, 7),
               new Worker("Jivko", "Denchev", 680.58m, 9),
               new Worker("Rosi", "Yankova", 240m, 14),
               new Worker("Rumen", "Zaimov", 315.68m, 18),
               new Worker("Georgi", "Borukov", 110.80m, 2),
               new Worker("Oleg", "Pavlov", 500m, 5),
               new Worker("Vanya", "Vencheva", 190.38m, 12),
               };

            //sorting students by marks ascending using LINQ
            var sortStudents = from st in students
                               orderby st.Grade
                               select st;

            foreach (var st in sortStudents)
            {
                Console.WriteLine(st);
            }

            Console.WriteLine();

            //the same using extension methods
            var sortedStud = students.OrderBy(st => st.Grade);

            foreach (var st in sortedStud)
            {
                Console.WriteLine(st);
            }

            Console.WriteLine();
            //sorting workers by money per hour in descending order
            var sortWorkers = from wks in workers
                              orderby wks.MoneyPerHour() descending
                              select wks;

            foreach (var wrk in sortWorkers)
            {
                Console.WriteLine(wrk);
            }

            Console.WriteLine();
            //the same task using extension methods
            var sortedWorkers = workers.OrderByDescending(wrk => wrk.MoneyPerHour());

            foreach (var worker in sortedWorkers)
            {
                Console.WriteLine(worker);
            }

            Console.WriteLine();
            //merging the lists and sorting them by first name and last name
            var merged = students.Concat<Human>(workers).OrderBy(hm => hm.FirstName).ThenBy(hm => hm.LastName);

            foreach (var human in merged)
            {
                Console.WriteLine(human.FirstName +" "+ human.LastName);
            }
        }
Esempio n. 6
0
        private static IEnumerable<Student> StudentsGenerator()
        {
            var result = new Student[ARRAY_SIZE];
            for (int index = 0; index < ARRAY_SIZE; index++)
            {
                result[index] = SingleStudentGenerator();
            }

            return result;
        }
        static void Main()
        {
            Student gosho = new Student("Gosho", "Goshov", 4);
            Student pesho = new Student("Pesho", "Peshov", 5);
            Student minka = new Student("Minka", "Goshova", 6);
            Student mariika = new Student("Mariq", "Toshova", 5);
            Student tosho = new Student("Todor", "Todorov", 6);
            Student ivancho = new Student("Ivan", "Kalpazanov", 8);
            Student miro = new Student("Miroslav", "Goshov", 3);
            Student stamat = new Student("Stamat", "Stamatov", 4);
            Student vasil = new Student("Vasil", "Petrov", 4);
            Student stoqn = new Student("Stoqn", "Hristov", 4);

            List<Student> studentsList = new List<Student>();
            studentsList.Add(gosho);
            studentsList.Add(pesho);
            studentsList.Add(minka);
            studentsList.Add(mariika);
            studentsList.Add(tosho);
            studentsList.Add(ivancho);
            studentsList.Add(miro);
            studentsList.Add(stamat);
            studentsList.Add(vasil);
            studentsList.Add(stoqn);

            var sortedStudentList = studentsList.OrderBy(s => s.Grade);

            Console.WriteLine("Students:");
            Console.WriteLine();

            foreach (var item in sortedStudentList)
            {
                Console.WriteLine("{0,-10} - grade: {1}", item.FirstName, item.Grade);
            }

            Console.WriteLine(new string('_', 20));
            Console.WriteLine();

            Worker filip = new Worker("Kristian", "Tumbev", 50, 8);
            Worker kolio = new Worker("Nikola", "Nikolov", 20, 4);
            Worker hristo = new Worker("Hristo", "Iordanov", 100, 8);
            Worker dancho = new Worker("Iordan", "Vylkov", 100, 8);
            Worker sasho = new Worker("Aleksandyr", "Uzunov", 100, 2);
            Worker georgi = new Worker("Georgi", "Georgiev", 60, 8);
            Worker ceco = new Worker("Cvetomir", "Nachev", 100, 8);
            Worker presiqn = new Worker("Presiqn", "Aleksandrov", 100, 4);
            Worker detelin = new Worker("Detelin", "Tumbev", 80, 8);
            Worker cveta = new Worker("Cvetomira", "Nacheva", 150, 8);

            List<Worker> listWorkers = new List<Worker>();

            listWorkers.Add(filip);
            listWorkers.Add(kolio);
            listWorkers.Add(hristo);
            listWorkers.Add(dancho);
            listWorkers.Add(sasho);
            listWorkers.Add(georgi);
            listWorkers.Add(ceco);
            listWorkers.Add(presiqn);
            listWorkers.Add(detelin);
            listWorkers.Add(cveta);

            var sortedListWorkers = listWorkers.OrderByDescending(w => w.MoneyPerHour());

            Console.WriteLine("Workers:");
            Console.WriteLine();

            foreach (var item in sortedListWorkers)
            {
                Console.WriteLine("{0,-10} - money per hour: {1}", item.FirstName, item.MoneyPerHour());
            }

            var both = new List<Human>();

            both.AddRange(sortedStudentList);
            both.AddRange(sortedListWorkers);

            var sortedBoth = both.OrderBy(b => b.FirstName).ThenBy(b => b.LastName);

            Console.WriteLine();
            Console.WriteLine(new string('-', 10));
            Console.WriteLine();
            Console.WriteLine("Students and workers:");
            Console.WriteLine();

            foreach (var item in sortedBoth)
            {
                Console.WriteLine("{0,-10} {1}", item.FirstName, item.LastName);
            }
        }