Currently, I'm studying for C#.
I've been asking questions about practice since I've never had it before.
I'm stuck in sorting.
Use the following code:
:1. Create a list for men only
2. Create a list for women only
3. Arrange A to H of each list in ascending order
I would like to implement this process.
I looked it up on the Internet, but
I don't know how to do it.
Could you please let me know?
class Person
{
public string name;
public stringGender;
public Person (string name, string gender)
{
This.Name=name;
This.Gender=gender;
}
}
class program
{
static void Main (string[]args)
{
List<string>list=new List<string>();;
list.Add("E woman");
list.Add ("F woman");
list.Add ("G Man");
list.Add ("H-man");
list.Add("Woman A";
list.Add("B Man";
list.Add ("Woman C");
list.Add("D Man";
List<Person>People=new List<Person>();
foreach(string1 in list)
{
string [ ] splitdata = s1.Split(');
People.Add (new Person (splitdata[0], splitdata[1]);
}
foreach (Person in People)
{
Console.WriteLine("{0}{1}", a.Name, a.Gender);
}
}
}
Also, sorting does not use LINQ.
Repeat and branch as much as possible
I'd like to rearrange it.
class Person
{
public string name;
public stringGender;
public Person (string name, string gender)
{
This.Name=name;
This.Gender=gender;
}
public static Person FromString (string text)
{
var array=text.Split(');
return new Person (array[0], array[1]);
}
}
static void Main (string[]args)
{
var list = new List <string >
{
"Woman E,"
"Woman F,"
"Man G,"
"H man,"
"Woman A,"
"Man B,"
"Woman C,"
"D man,"
};
varpeople=list.Select(_=>Person.FromString(_)) .ToList();
Console.WriteLine("people");
Display (people);
Console.WriteLine();
varmales=people.Where(_=>_.Gender=="Man").ToList();
OrderByName(males);
Console.WriteLine("males");
Display(males);
Console.WriteLine();
varfemales=people.Where(_=>_.Gender=="Woman").ToList();
OrderByName(females);
Console.WriteLine("females");
Display (females);
Console.WriteLine();
}
static void display (IEnumerable<Person>people)
{
foreach (var person in people)
{
Console.WriteLine($"{person.Name}{person.Gender}");
}
}
static void OrderByName (List<Person>people)
{
for (vari=0;i<people.Count-1;i++)
{
for (varj=people.Count-1;j>i;j--)
{
if (people[j].Name[0]<people[j-1].Name[0])
{
variant=people[j-1];
people[j-1] = people[j];
people[j] = temp;
}
}
}
}
and so on.
using System;
using System.Collections.General;
using System.Linq;
using System.Text;
namespace Sample
{
public class person
{
public string name {get;set;}
public stringGender {get;set;}
public EGender EnumedGender {get;set;}
// If the value you enter is fixed with a particular one, it would be better to use this enumeration.
public enum EGender
{
Male,
Female
}
public Person(string_name, EGender_gender)
{
Name =_name;
EnumedGender=_gender;
}
public Person(string_name, string_gender)
{
Name =_name;
Gender=_gender;
}
// Overwriting the ToString method to display the contents of the instance as a string.
public override string ToString()
{
return Name + " + Gender;
}
}
class program
{
static void Main (string[]args)
{
// Block 1. Create an unorganized list
var list = new List <Person >()
{
new Person ("E", "Woman"),
new Person ("F", "Woman"),
new Person ("G", "Man"),
new Person ("H", "Man"),
new Person ("A", "Woman"),
new Person ("B", "Man"),
new Person ("C", "Woman"),
new Person ("D", "Man")
};
// Block 2. Make a list for each male and woman
varmaleList = new List <Person >();
varfemaleList = new List <Person >();
// Block 3. Separate men and women
foreach (var person in list)
{
switch(person.Gender)
{
case "Man":
maleList.Add (person);
break;
case "Woman":
femaleList.Add (person);
break;
}
}
// Organize each list.
maleList=asort(maleList).ToList();
femaleList=asort(femaleList).ToList();
Display(maleList);
Console.WriteLine("---------------------------------");
Display(femaleList);
}
// The sorting part is troublesome, so it is omitted.
private static IEnumerable<Person>asort (IEnumerable<Person>target)
{
return target.OrderBy(val=>val.Name);
}
// Output.
private static void display (IEnumerable<Person>target)
{
foreach (var person into target)
{
Console.WriteLine (person);
}
}
}
}
Today's sorting itself is becoming a standard function of various languages, and if you leave it to someone else...
There are some problems with the code provided, so I would like to ask you about it.
© 2024 OneMinuteCode. All rights reserved.