Hi
Been struggling to solve this for quite a long time now.. Nowhere on the internet can I find a solution for this.
Please find below a hypothetical example of what I have..
I'm sorting a List<> of "Persons" (consists of name, age and city). This is no problem whatsoever for sorting actions that sort only 1 parameter at a time. Example:
Now what I'm looking for is an output like this:
So sort the list by name first, and after that sort the already sorted list with a second parameter age. In this example that means that Albert with the age of 35 comes first, over Albert with the age of 41.
Doing this:
(obviously) results in the same list as the one sorted by age only.
Does anybody have any idea how I can achieve this?
Thanks in advance,
René
Been struggling to solve this for quite a long time now.. Nowhere on the internet can I find a solution for this.
Please find below a hypothetical example of what I have..
I'm sorting a List<> of "Persons" (consists of name, age and city). This is no problem whatsoever for sorting actions that sort only 1 parameter at a time. Example:
[COLOR=#0000ff][COLOR=#0000ff]public [/COLOR][/COLOR][COLOR=#0000ff][COLOR=#0000ff]class [/COLOR][/COLOR][COLOR=#2b91af][COLOR=#2b91af]Person[/COLOR]
[/COLOR]{
[COLOR=#0000ff][COLOR=#0000ff] public [/COLOR][/COLOR][COLOR=#0000ff][COLOR=#0000ff]int[/COLOR][/COLOR] age;
[COLOR=#0000ff][COLOR=#0000ff] public [/COLOR][/COLOR][COLOR=#0000ff][COLOR=#0000ff]string[/COLOR][/COLOR] name;
[COLOR=#0000ff][COLOR=#0000ff] public [/COLOR][/COLOR][COLOR=#0000ff][COLOR=#0000ff]string[/COLOR][/COLOR] city;
[COLOR=#0000ff][COLOR=#0000ff] public[/COLOR][/COLOR] Person([COLOR=#0000ff][COLOR=#0000ff]int[/COLOR][/COLOR] age, [COLOR=#0000ff][COLOR=#0000ff]string[/COLOR][/COLOR] name, [COLOR=#0000ff][COLOR=#0000ff]string[/COLOR][/COLOR] city)
{
[COLOR=#0000ff][COLOR=#0000ff] this[/COLOR][/COLOR].age = age;
[COLOR=#0000ff][COLOR=#0000ff] this[/COLOR][/COLOR].name = name;
[COLOR=#0000ff][COLOR=#0000ff] this[/COLOR][/COLOR].city = city;
}
}
[COLOR=#0000ff][COLOR=#0000ff]static [/COLOR][/COLOR][COLOR=#0000ff][COLOR=#0000ff]void[/COLOR][/COLOR] Main([COLOR=#0000ff][COLOR=#0000ff]string[/COLOR][/COLOR][] args)
{
[COLOR=#2b91af][COLOR=#2b91af]List[/COLOR][/COLOR]<[COLOR=#2b91af][COLOR=#2b91af]Person[/COLOR][/COLOR]> lstPersons = [COLOR=#0000ff][COLOR=#0000ff]new[/COLOR][/COLOR][COLOR=#2b91af][COLOR=#2b91af]List[/COLOR][/COLOR]<[COLOR=#2b91af][COLOR=#2b91af]Person[/COLOR][/COLOR]>();
lstPersons.Add([COLOR=#0000ff][COLOR=#0000ff]new [/COLOR][/COLOR][COLOR=#2b91af][COLOR=#2b91af]Person[/COLOR][/COLOR](37, [COLOR=#a31515][COLOR=#a31515]"Peter"[/COLOR][/COLOR], [COLOR=#a31515][COLOR=#a31515]"Amsterdam"[/COLOR][/COLOR]));
lstPersons.Add([COLOR=#0000ff][COLOR=#0000ff]new [/COLOR][/COLOR][COLOR=#2b91af][COLOR=#2b91af]Person[/COLOR][/COLOR](24, [COLOR=#a31515][COLOR=#a31515]"René"[/COLOR][/COLOR], [COLOR=#a31515][COLOR=#a31515]"Den Bosch"[/COLOR][/COLOR]));
lstPersons.Add([COLOR=#0000ff][COLOR=#0000ff]new [/COLOR][/COLOR][COLOR=#2b91af][COLOR=#2b91af]Person[/COLOR][/COLOR](41, [COLOR=#a31515][COLOR=#a31515]"Albert"[/COLOR][/COLOR], [COLOR=#a31515][COLOR=#a31515]"Paris"[/COLOR][/COLOR]));
lstPersons.Add([COLOR=#0000ff][COLOR=#0000ff]new [/COLOR][/COLOR][COLOR=#2b91af][COLOR=#2b91af]Person[/COLOR][/COLOR](35, [COLOR=#a31515][COLOR=#a31515]"Albert"[/COLOR][/COLOR], [COLOR=#a31515][COLOR=#a31515]"Paris"[/COLOR][/COLOR]));
[COLOR=#2b91af][COLOR=#2b91af]Console[/COLOR][/COLOR].WriteLine([COLOR=#a31515][COLOR=#a31515]"Sorting by age"[/COLOR][/COLOR]);
lstPersons.Sort([COLOR=#0000ff][COLOR=#0000ff]delegate[/COLOR][/COLOR]([COLOR=#2b91af][COLOR=#2b91af]Person[/COLOR][/COLOR] p1, [COLOR=#2b91af][COLOR=#2b91af]Person[/COLOR][/COLOR] p2) { [COLOR=#0000ff][COLOR=#0000ff]return[/COLOR][/COLOR] p1.age.CompareTo(p2.age); });
[COLOR=#0000ff][COLOR=#0000ff]foreach[/COLOR][/COLOR] ([COLOR=#2b91af][COLOR=#2b91af]Person[/COLOR][/COLOR] p [COLOR=#0000ff][COLOR=#0000ff]in[/COLOR][/COLOR] lstPersons)
[COLOR=#2b91af][COLOR=#2b91af]Console[/COLOR][/COLOR].WriteLine([COLOR=#a31515][COLOR=#a31515]"Name: {0} - Age: {1} - City: {2}"[/COLOR][/COLOR], p.name, p.age, p.city);
[COLOR=#008000][COLOR=#008000]/* OUTPUT:[/COLOR]
[COLOR=#008000]* Sorting by name[/COLOR]
[COLOR=#008000]Name: René - Age: 24 - City: Den Bosch[/COLOR]
[COLOR=#008000]Name: Albert - Age: 35 - City: Paris[/COLOR]
[COLOR=#008000]Name: Peter - Age: 37 - City: Amsterdam[/COLOR]
[COLOR=#008000]Name: Albert - Age: 41 - City: Paris[/COLOR]
[COLOR=#008000]* */[/COLOR]
[/COLOR]
[COLOR=#2b91af][COLOR=#2b91af]Console[/COLOR][/COLOR].WriteLine([COLOR=#2b91af][COLOR=#2b91af]Environment[/COLOR][/COLOR].NewLine);
[COLOR=#2b91af][COLOR=#2b91af]Console[/COLOR][/COLOR].WriteLine([COLOR=#a31515][COLOR=#a31515]"Sorting by name"[/COLOR][/COLOR]);
lstPersons.Sort([COLOR=#0000ff][COLOR=#0000ff]delegate[/COLOR][/COLOR]([COLOR=#2b91af][COLOR=#2b91af]Person[/COLOR][/COLOR] p1, [COLOR=#2b91af][COLOR=#2b91af]Person[/COLOR][/COLOR] p2) { [COLOR=#0000ff][COLOR=#0000ff]return[/COLOR][/COLOR] p1.name.CompareTo(p2.name); });
[COLOR=#0000ff][COLOR=#0000ff]foreach[/COLOR][/COLOR] ([COLOR=#2b91af][COLOR=#2b91af]Person[/COLOR][/COLOR] p [COLOR=#0000ff][COLOR=#0000ff]in[/COLOR][/COLOR] lstPersons)
[COLOR=#2b91af][COLOR=#2b91af]Console[/COLOR][/COLOR].WriteLine([COLOR=#a31515][COLOR=#a31515]"Name: {0} - Age: {1} - City: {2}"[/COLOR][/COLOR], p.name, p.age, p.city);
[COLOR=#008000][COLOR=#008000]/* OUTPUT:[/COLOR]
[COLOR=#008000]* Sorting by age[/COLOR]
[COLOR=#008000][COLOR=#008000][COLOR=#008000]Name: Albert - Age: 41 - City: Paris[/COLOR]
[COLOR=#008000]Name: Albert - Age: 35 - City: Paris[/COLOR]
[COLOR=#008000]Name: Peter - Age: 37 - City: Amsterdam[/COLOR]
[COLOR=#008000]Name: René - Age: 24 - City: Den Bosch[/COLOR]
[/COLOR]* */
[/COLOR][/COLOR]
[COLOR=#008000]
[/COLOR][COLOR=#2b91af][COLOR=#2b91af] Console[/COLOR][/COLOR].ReadLine();
}
Now what I'm looking for is an output like this:
[COLOR=#008000][COLOR=#008000] /* OUTPUT SHOULD BE:[/COLOR]
[COLOR=#008000] * Sort by name, then by age[/COLOR]
[COLOR=#008000] Name: Albert - Age: 35 - City: Paris [/COLOR]
[COLOR=#008000] Name: Albert - Age: 41 - City: Paris[/COLOR]
[COLOR=#008000] Name: Peter - Age: 37 - City: Amsterdam[/COLOR]
[COLOR=#008000] Name: René - Age: 24 - City: Den Bosch[/COLOR]
[COLOR=#008000] * */[/COLOR]
[/COLOR]
So sort the list by name first, and after that sort the already sorted list with a second parameter age. In this example that means that Albert with the age of 35 comes first, over Albert with the age of 41.
Doing this:
[SIZE=2] lstPersons.Sort([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]delegate[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Person[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] p1, [/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Person[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] p2) { [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]return[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] p1.name.CompareTo(p2.name); });
lstPersons.Sort([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]delegate[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Person[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] p1, [/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Person[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] p2) { [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]return[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] p1.age.CompareTo(p2.age); });
[/SIZE][COLOR=#008000][/COLOR]
(obviously) results in the same list as the one sorted by age only.
Does anybody have any idea how I can achieve this?
Thanks in advance,
René