Personクラスの両方が含まれているクラスリストソートする:私は、次のようなCSVファイルから文字列データをソートすることができる午前どのようにダブル、文字列データ
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('"'));
}
}
}
}
}
:
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;
}
マイプログラムコードを名前ですが、年齢と身分証明書で正しい注文(昇順または降順)を取得できません 私はこの問題について多くのことを検索しました。 IcomparableおよびTryparse を使用しようとしましたが、肯定的な結果はありません。問題は、Orderbyが年齢とIDを文字列として処理することです。しかし、AgeとIDをdoubleまたはintと定義すると、temp配列を定義しているときに "変換できません"ということになります。任意の有益な提案や解決をお願いします。
これは、たとえば年齢に従って注文した場合に発生します。それはまだIDによると発注しているようだ!代わりに、式の
コードの壁は読みにくい(特に、垂直方向の間隔が広すぎる)。あなたの問題を示す*最小の例に減らしてください。 – Richard
複数の新しいリストを作成してから、それらのリストに割り当てる必要はありません。 'var personList_name =新しいリスト(); personList_name = personList.OrderBy(...); '不必要に新しいリストを割り当てて、そこに2番目の行を上書きします。また、 'TryParse'を含む' OrderBy'は 'true' /' false'メソッドの結果で順序付けされ、解析された値ではありません。彼らが正常に解析すると、それらは 'true'によって順序付けられます。解析に失敗したものは 'false'によって順序付けられます。 (昇順)順序では、 'false'は' true'の前に来ます。その後、結果はおそらく(?)ランダムです。 –
pinkfloydx33