Как отсортировать список людей по полу? - C#
Формулировка задачи:
Есть список людей:
Петров
Иванов
Жеглова
Васичкин
Огурцова
Нужно распределить их по статьям, мужской и женской.
Решение задачи: «Как отсортировать список людей по полу?»
textual
Листинг программы
using System;
using System.Collections.Generic;
namespace ConsoleAppTest
{
public struct Human
{
public bool IsMale { set; get; }
public string Name { set; get; }
public Human(string name, bool isMale) : this() {
this.Name = name;
this.IsMale = isMale;
}
}
class MainClass
{
public static void Main(string[] args) {
List<Human> names = new List<Human> {
new Human("Петров", true),
new Human("Иванов", true),
new Human("Жеглова", false),
new Human("Васичкин", true),
new Human("Огурцова", false)
};
List<string> females = new List<string>(); // женщины
List<string> males = new List<string>(); // мужчины
foreach (Human human in names) {
if (human.IsMale)
males.Add(human.Name);
else
females.Add(human.Name);
}
// вывод
Console.WriteLine("Мужчины:");
foreach (string male in males)
Console.WriteLine(male);
Console.WriteLine("\nЖенщины:");
foreach (string female in females)
Console.WriteLine(female);
}
}
}