Answer the question
In order to leave comments, you need to log in
Constant field when sorting?
Hello.
This is how I display data from the text in the dataGridView.
public Uefa(int number, string fio, string club, string role, int games, int goal, int assist)
{
this.number = number;
this.fio = fio;
this.club = club;
this.role = role;
this.games = games;
this.goal = goal;
this.assist = assist;
}
public static List<Uefa> getList()
{
// структура
List<Uefa> list = new List<Uefa>();
// массив данных
string[] arr = System.IO.File.ReadAllLines(filename, Encoding.Default);
for (int i = 0; i < arr.Length; i++)
{
// разбивка на подстроки
string[] line = arr[i].Split(' ');
Uefa data = new Uefa(list.Count + 1, line[0], line[1], line[2], Convert.ToInt32(line[3]), Convert.ToInt32(line[4]), Convert.ToInt32(line[5]));
// заполняем структуру
list.Add(data);
}
return list;
}
}
dataGridView1.DataSource = null;
list = list.OrderBy(x => x.club).ToList<Uefa>();
Answer the question
In order to leave comments, you need to log in
In your version, objects
are sorted whose fields are club and number. Sort by the club field, then the number field of the same object will "float away" with it.
To change the numbering order by a new one, then recalculate it after the next sorting.
int i=0;
foreach(var item in list) item.number = i++;
int i=0;
list.Select(l => l.number = i++);
int i=0;
foreach(var item in list) Console.Writeline("{0}.{1}", i++, item.ToString());
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question