N
N
Nikolai Novosad2016-01-27 03:44:10
C++ / C#
Nikolai Novosad, 2016-01-27 03:44:10

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;

}
}

number in the file. This is the serial number of the entry.
This is how I sort.
dataGridView1.DataSource = null;
list = list.OrderBy(x => x.club).ToList<Uefa>();

The data is sorted, but the sequence number is also sorted. Those. comes out 3,4,1,5.
How to fix the ordinal so it doesn't get sorted?
Thanks in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
eRKa, 2016-01-27
@kttotto

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++;

or
int i=0;
list.Select(l => l.number = i++);

In general, if you do not need to display the serial number of a single object somewhere, but indicate the serial number only when displaying the entire list, then there is no point in storing the serial number in the object.
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 question

Ask a Question

731 491 924 answers to any question