Person class:
class Person
{
public string ID;
public string Name;
public string PClass;
public string Age;
public string Sex;
public string Survived;
public string SexCode;
public Person(string id,string name,string pclass,string age,string sex,
string survived,string sexcode)
{
ID = id;
Name = name;
PClass = pclass;
Age = age;
Sex = sex;
Survived = survived;
SexCode = sexcode;
}
My program code:
class Program
{
static void Main(string[] args)
{
string[] data = File.ReadAllLines("titanic.csv");
data = data.Skip(1).ToArray();
List<Person> personList = new List<Person>();
List<Person> personList_name = new List<Person>();
List<Person> personList_pclass = new List<Person>();
List<Person> personList_age = new List<Person>();
List<Person> personList_sex = new List<Person>();
List<Person> personList_id = new List<Person>();
for (int i = 0; i < data.Length; i++)
{
string[] temp = Regex.Split(data[i], ",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)");
Person person = new Person(temp[0], temp[1], temp[2],temp[3],
temp[4], temp[5], temp[6]);
personList.Add(person);
}
personList_name = personList.OrderBy(x => x.Name).ToList();
personList_pclass = personList.OrderBy(z => z.PClass).ToList();
personList_sex = personList.OrderBy(w => w.Sex).ToList();
int id_;
int age_;
personList_age = personList.OrderBy(y => int.TryParse(y.Age, out age_)).ToList();
//personList_id = personList.OrderByDescending(int.TryParse(ID, out number)).ToList();
personList_id = personList.OrderBy(o => int.TryParse(o.ID, out id_)).ToList();
while (true)
{
Console.WriteLine(" Please select your filtring method:\n" +
"1-By Name\n2-By Pclass\n3-By Age\n4-By Sex\n5-By ID\n Press -1 to quit.");
string selection = Console.ReadLine();
if (selection == "-1")
{
break;
}
Console.WriteLine(("{0,-10}{1,70}{2,20}{3,20}{4,20}{5,20}{6,20}"), item.ID.Trim('"'), item.Name.Trim('"'), item.PClass.Trim('"')
, item.Age, item.Sex.Trim('"'), item.Survived.Trim('"')
, item.SexCode.Trim('"'));
}
}
if (selection == "3")
{
Console.WriteLine(("{0,-10}{1,70}{2,20}{3,20}{4,20}{5,20}{6,20}"), "ID", "NAME", "PCLASS", "AGE", "SEX", "SURVIVED", "SEXCODE");
foreach (var item in personList_age)
{
Console.WriteLine(("{0,-10}{1,70}{2,20}{3,20}{4,20}{5,20}{6,20}"), item.ID.Trim('"'), item.Name.Trim('"'), item.PClass.Trim('"')
, item.Age, item.Sex.Trim('"'), item.Survived.Trim('"')
, item.SexCode.Trim('"'));
}
}
}
}
}
I am able to sort string data from CSV file such as Name, but for age and ID I cannot obtain the correct order (either ascending or descending) I have searched a lot about this problem. Tried to use Icomparable and Tryparse but without any positive result. The problem is that Orderby deals with age and ID as string. But if I define Age and ID as double or int I will have "cannot convert" while defining temp array. Any helpful suggestion or solution PLEASE?
This is what happens when I order according to Age for example. It seems still ordering according to ID!
(source: up-00.com)
Instead of an expression, provide a function:
Or to keep things clean maybe write an extension method:
And call like this: