1

I've searched StackOverflow for an answer to this, but can't find a clear answer.

I've this Player Class (Yes i've posted the class before)

namespace Tennis_Match
{
class Player
{
    private string first_name;
    private string middle_name;
    private string last_name;
    private DateTime dob;
    private string nat;
    private char gender;
    public string First_name { get { return first_name; } set { first_name = value; } }
    public string Middle_name { get {return middle_name; } set { middle_name = value; } }
    public string Last_name { get { return last_name; } set { last_name = value; } }
    public DateTime Dob { get { return dob; } set { dob = value; } }
    public string Nat { get { return nat; } set { nat = value; } }
    public char Gender { get { return gender; } set { gender = value; } }
    public Player(string first_name, string last_name, string middle_name, DateTime dob, string nat, char gender)
    {
        this.first_name = first_name;
        this.last_name = last_name;
        this.middle_name = middle_name;
        this.dob = dob;
        this.nat = nat;
        this.gender = gender;
    }
    public override string ToString()
    {
        return first_name + " " + middle_name + " " + last_name + " " + dob + " "+ nat + " " + gender;
    }

    public static int CalculateAge(DateTime dob)
    {
        int years = DateTime.Now.Year - dob.Year;

        if ((dob.Month > DateTime.Now.Month) || (dob.Month == DateTime.Now.Month && dob.Day > DateTime.Now.Day))
            years--;

        return years;
    }

    private List<string> content = new List<string>();
    public string FileName { get; set; }
    public string Delimiter { get; set; }
    private void Load()
    {
        TextFieldParser par = new TextFieldParser(FileName);
        par.TextFieldType = FieldType.Delimited;
        par.SetDelimiters(Delimiter);
        while (!par.EndOfData)
        {
            string[] fields = par.ReadFields();
            foreach (string field in fields)
            {
                Console.WriteLine(field);
            }
        }
        par.Close();
    }
    public void RunReadCSVFile(string fn, string delim = "|")
    {
        FileName = fn;
        Delimiter = delim;
        Load();
    }
    public string GetLine(string fileName, int line)
    {
        using (var sr = new StreamReader(fileName))
        {
            for (int i = 1; i < line; i++)
                sr.ReadLine();
            return sr.ReadLine();
        }
    }
}
}

Then I've another class tournament. I want to sort a textfile by among other Last_name. I've got an idea that i might need to use IComparable to sort the file. The file is structured like this:

1|Mariuss|Luka|Thygesen|1986-07-25|NAURU|NR
  • What do you mean by "sort a textfile"? You want to read data from a text file and sort the items in memory? Or you want to change the contents of the file? – Evan Trimboli May 08 '16 at 11:08
  • Whoops... read the data and then display the files sorted by Last_name –  May 08 '16 at 11:11
  • Assuming you've got working code to read the file, then it's a duplicate of http://stackoverflow.com/questions/3309188/how-to-sort-a-listt-by-a-property-in-the-object – Evan Trimboli May 08 '16 at 11:14
  • There is no possibbilities to "sort file". You have to read data, sort in memory as @Evan and eventually save to file sorted data. To sort in memory you can use Linq or SortedList during file reading. – Klaudiusz bryjamus May 08 '16 at 11:21
  • I've done some work myself, and tried to do this: `List lines = File.ReadLines(@"C:\Users\Johannes\Downloads\BAIT\GOOP\Eksamen\MalePlayer.txt").ToList(); public List GetPlayerList(IEnumerable lines, Func Last_name) { return (from Player in lines orderby Last_name(Player) select Player).ToList(); } svm var playersOrderedByLastName = GetPlayerList(lines, x => x.Last_name);` But i get an error, so I suppose it's not right –  May 08 '16 at 12:05

1 Answers1

1

First you have nothing to sort. So add to class

 static List<Player> players = new List<Player>();

Next you need to add items to List in following function

       private void Load()
        {
            TextFieldParser par = new TextFieldParser(FileName);
            par.TextFieldType = FieldType.Delimited;
            par.SetDelimiters(Delimiter);
            while (!par.EndOfData)
            {
                string[] fields = par.ReadFields();
                foreach (string field in fields)
                {
                    Console.WriteLine(field);
                }
                //-----------------------------Add -----------------------
                Player newPlayer = new Player(fields[0], fields[1], fields[2], DateTime.Parse(fields[3]), fields[4], fields[5][0]);
                players.Add(newPlayer);
            }
            par.Close();
        }

Now you have something to sort. So add a sort method (or CompareTo())

public void Sort()
        {
            players = players.OrderBy(x => new { x.last_name, x.first_name, x.middle_name }).ToList();
        }
jdweng
  • 33,250
  • 2
  • 15
  • 20
  • What to do in the Main then, to write it to console? For example display it sorted by last_name/first_name –  May 08 '16 at 12:49
  • Do what you needed to do with it in the first place. You didn't say in your question what you needed it for. – Monset May 08 '16 at 12:54
  • Either use the sort method in the class or sort the public property 'players'. You didn't post the main method so I don't know what you are doing. It is better to have a static item players in the class then to have it in the main method. If you already have a list of players in main then sort the list that exists. – jdweng May 08 '16 at 13:09
  • So far I've got nothing in the Main: `static void Main(string[] args) { /*AddMalePlayer(3); AddFemalePlayer(7); AddReferee(9);*/ players.Sort(); }` When I write that, I doesn't write anything to the console –  May 08 '16 at 13:13
  • You need a write() method that takes 'players' and writes to console. Right now only the load method writes to the console. – jdweng May 08 '16 at 13:21
  • not sure what you mean. If the Load method write to the console, why can't I just call the method? –  May 08 '16 at 13:31
  • but what to do then? `static void Main(string[] args) { } ` If I can't call Load method or write players.sort(). Then I don't get how to print on the screen –  May 08 '16 at 14:05
  • Try this : foreach (Player player in players) { Console.WriteLine(player.ToString()); } – jdweng May 08 '16 at 17:47
  • that gave me nothing. –  May 08 '16 at 18:31
  • It should of worked. Did you run it after you ran load? Did the load method produce output? Do you have this line of code : players.Add(newPlayer)? – jdweng May 08 '16 at 21:29