The online racing simulator
C# Sorting Lists help
(19 posts, started )
C# Sorting Lists help
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:


[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é
Dirtiest way ever? Concatenate the name and age and then compare.

return (p1.name + p1.age).CompareTo(p2.name + p2.age);

That way it will be comparing Albert35 with Albert41 and mr 35 wins.

EDIT:
For clarification though - you just have to handle it all by calling Sort once - whatever happens during the delegation sets the sorting for the list. If you call it again then naturally it will re-sort it based on the new criteria. Ideally you should have a decent method to delegate the handling all the situations or combinations to.
Quote from xaotik :Dirtiest way ever? Concatenate the name and age and then compare.

Hahaha great stuff, that'll do on my last day before holidays!

I do hope that in the mean time someone else will find a less dirty way!

Thanks a lot xaotik!!
You could also make your person class implement the comparable interface, so you define yourself how two persons compare to each other

public class person : IComparable

The above should give you a template for the Compare method.

EDIT:

More info and examples here:

http://en.csharp-online.net/IComparable
Doesn`t the sort method accept a class that implements IComparer?

If so just create your own compare class that implements IComparer..Check out MSDN for examples of this.

This class also can have properties to allow you to set what parts of a 'person' are checked before others

Im away at the moment so if you need the code knocking up you will have to wait until Saturday!!

you beat me to it burnout!!
Yeah I figured the word IComparer would come up quite a lot here. Unfortunately I haven't really got my head around that yet.. Seems like I will have to asap though!

Thanks for pointing me in the right direction guys
are you happy with what an interface is?
Quote from MrRoper :are you happy with what an interface is?

I've heard of the word, but my theoretical knowledge of C# is nigh to zero. I usually only dive into the theory when I get stuck with my existing knowledge (like right now with the IComparer). BurnOut69 keeps telling me I have to read my C# bible though
OK so a quick interface tutorial!
im not at home though so the code will be untested!

An interface is just a definition of a "contract" i.e. it allows the creator of an interface to define a contract that consumers of the interface must adhear to. now the beauty of an interface is that it doesnt care about the underlying types. As long as an object implements the interface, you can interact with that object without caring what type the object actually is.

Example

I have a load of different vehicle types and they can all be started so i define an iStartable interface. The interface has a start method defined that returns true or false

interface iStartable
{
bool Start();
}

I can now implement this with different types of vehicles

class Car : iStartable
{
public bool Start()
{
debug.writeline("Car Started");
}
}

class Truck : iStartable
{
public bool Start()
{
debug.writeline("Truck Started");
}
}

the good thing is i dont need to worry about their concrete type so for example i can do the following

arraylist myList = new arrayList();
myList.Add (new car());
mylist.Add(new Truck());

foreach(iStartable startable in myList)
{
startable.Start();
}



does that make any sense???
Quote from MrRoper :does that make any sense???

It makes perfect sense actually, thanks for that!

The biggest difficulty will be to make an algorythm for the comparison though.. That's not gonna be today or the upcoming 10 days.. but I will definitely look into this, soon as I'm back from holidays.

Thanks again!

:surfing:
The code isnt that hard for the comparer,

Im happy to knock one up for you at the weekend if you like?
Quote from MrRoper :The code isnt that hard for the comparer,

Im happy to knock one up for you at the weekend if you like?

Thanks for the offer man Have to say I normally want to give it a try myself first, but a good example would actually be great in this case. I'll send you my Class via pm.
#14 - Woz
If you have a fixed max length on your name fields the quickest and safest way is to space extend the name and then for the ages 0 pad the age based on max age.

This sort of stuff comes up a lot and you either have to start building collections of collections etc or make a key. A key/hash is always fastest BUT you make to make sure your data is constrained.

So if name is 10 chars max and age is 150 max given the following data.

Bill age 20
Tom age 30
Jill Smith age 5

you would do the following keys

Bill 020
Tom 030
Jill Smith005

Your sort will now ALWAYS be correct eben if the names have numbers etc. Without the zero padding on the ages you get all sorts of crap where 20 < 5 because it would character compare 2 and 5 etc.

HTH
#15 - Woz
Here is a solution where name is not fixed length but will cost a lot if the array is too large


public class Person
{
public int age;
public string name;
public string city;

public Person(int age, string name, string city)
{
this.age = age;
this.name = name;
this.city = city;
}


public static void SortNameAge(List<Person> listToSort)
{
if (listToSort.Count == 0)
{
// Bail if nothing to sort
return;
}

// Get max name length
int maxNameLength = 0;
foreach (Person person in listToSort)
{
if (person.name.Length > maxNameLength)
{
maxNameLength = person.name.Length;
}
}

listToSort.Sort(delegate(Person p1, Person p2)
{
string key1 = p1.name.PadRight(maxNameLength) + p1.age.ToString().PadLeft(5, '0');
string key2 = p2.name.PadRight(maxNameLength) + p2.age.ToString().PadLeft(5, '0');
return key1.CompareTo(key2);
});
}
}

i though of that one too woz but you dont have to pad the name becuase unless theyre exaclty the same the age doesnt matter for sorting
#17 - Woz
Quote from Shotglass :i though of that one too woz but you dont have to pad the name becuase unless theyre exaclty the same the age doesnt matter for sorting

True but you also have the issues with names that have numbers. Years of protecting against user stupidity have made me a very defensive programmer. So I automtically look for problems now if any of the data can come from end users

We have a term for GUI at work... Gross User Incompetence

You would not believe some of the bug reports that come down the line at work
this would be how I would do it



[SIZE=2][COLOR=#0000ff]public [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]class [/COLOR][/SIZE][SIZE=2][COLOR=#2b91af]Person[/COLOR][/SIZE][SIZE=2] : [/SIZE][SIZE=2][COLOR=#2b91af]IComparable[/COLOR][/SIZE][SIZE=2]<[/SIZE][SIZE=2][COLOR=#2b91af]Person[/COLOR][/SIZE][SIZE=2]>[/SIZE]
[SIZE=2]{[/SIZE]
[SIZE=2][COLOR=#0000ff] public [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][SIZE=2] age;[/SIZE]
[SIZE=2][COLOR=#0000ff] public [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]string[/COLOR][/SIZE][SIZE=2] name;[/SIZE]
[SIZE=2][COLOR=#0000ff] public [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]string[/COLOR][/SIZE][SIZE=2] city;[/SIZE]
[SIZE=2][COLOR=#0000ff] public[/COLOR][/SIZE][SIZE=2] Person([/SIZE][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][SIZE=2] age, [/SIZE][SIZE=2][COLOR=#0000ff]string[/COLOR][/SIZE][SIZE=2] name, [/SIZE][SIZE=2][COLOR=#0000ff]string[/COLOR][/SIZE][SIZE=2] city)[/SIZE]
[SIZE=2] {[/SIZE]
[SIZE=2][COLOR=#0000ff] this[/COLOR][/SIZE][SIZE=2].age = age;[/SIZE]
[SIZE=2][COLOR=#0000ff] this[/COLOR][/SIZE][SIZE=2].name = name;[/SIZE]
[SIZE=2][COLOR=#0000ff] this[/COLOR][/SIZE][SIZE=2].city = city;[/SIZE]
[SIZE=2] }[/SIZE]
[SIZE=2][COLOR=#0000ff] public [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][SIZE=2] CompareTo([/SIZE][SIZE=2][COLOR=#2b91af]Person[/COLOR][/SIZE][SIZE=2] other)[/SIZE]
[SIZE=2] {[/SIZE]
[SIZE=2][COLOR=#0000ff] int[/COLOR][/SIZE][SIZE=2] nameSort = [/SIZE][SIZE=2][COLOR=#0000ff]this[/COLOR][/SIZE][SIZE=2].name.CompareTo(other.name);[/SIZE]
[SIZE=2][COLOR=#0000ff] int[/COLOR][/SIZE][SIZE=2] ageSort = [/SIZE][SIZE=2][COLOR=#0000ff]this[/COLOR][/SIZE][SIZE=2].age.CompareTo(other.age);[/SIZE]
[SIZE=2][COLOR=#0000ff] if[/COLOR][/SIZE][SIZE=2] (nameSort == 0)[/SIZE]
[SIZE=2] {[/SIZE]
[SIZE=2][COLOR=#0000ff] return[/COLOR][/SIZE][SIZE=2] ageSort;[/SIZE]
[SIZE=2] }[/SIZE]
[SIZE=2][COLOR=#0000ff] else[/COLOR][/SIZE]
[SIZE=2] {[/SIZE]
[SIZE=2][COLOR=#0000ff] return[/COLOR][/SIZE][SIZE=2] nameSort;[/SIZE]
[SIZE=2] }[/SIZE]
[SIZE=2] }[/SIZE]
[SIZE=2]}[/SIZE]

Then just compare like you have


[SIZE=2][COLOR=#2b91af]Console[/COLOR][/SIZE][SIZE=2].WriteLine([/SIZE][SIZE=2][COLOR=#a31515]"Sorting by age"[/COLOR][/SIZE][SIZE=2]);[/SIZE]
[SIZE=2]lstPersons.Sort([/SIZE][SIZE=2][COLOR=#0000ff]delegate[/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#2b91af]Person[/COLOR][/SIZE][SIZE=2] p1, [/SIZE][SIZE=2][COLOR=#2b91af]Person[/COLOR][/SIZE][SIZE=2] p2) { [/SIZE][SIZE=2][COLOR=#0000ff]return[/COLOR][/SIZE][SIZE=2] p1.CompareTo(p2); });[/SIZE]
[SIZE=2][COLOR=#0000ff]foreach[/COLOR][/SIZE][SIZE=2] ([/SIZE][SIZE=2][COLOR=#2b91af]Person[/COLOR][/SIZE][SIZE=2] p [/SIZE][SIZE=2][COLOR=#0000ff]in[/COLOR][/SIZE][SIZE=2] lstPersons)[/SIZE]
[SIZE=2][COLOR=#2b91af] Console[/COLOR][/SIZE][SIZE=2].WriteLine([/SIZE][SIZE=2][COLOR=#a31515]"Name: {0} - Age: {1} - City: {2}"[/COLOR][/SIZE][SIZE=2], p.name, p.age, p.city);[/SIZE]

#19 - Woz
Quote from TheChad :this would be how I would do it



[SIZE=2][COLOR=#0000ff]public [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]class [/COLOR][/SIZE][SIZE=2][COLOR=#2b91af]Person[/COLOR][/SIZE][SIZE=2] : [/SIZE][SIZE=2][COLOR=#2b91af]IComparable[/COLOR][/SIZE][SIZE=2]<[/SIZE][SIZE=2][COLOR=#2b91af]Person[/COLOR][/SIZE][SIZE=2]>[/SIZE]
[SIZE=2]{[/SIZE]
[SIZE=2][COLOR=#0000ff] public [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][SIZE=2] age;[/SIZE]
[SIZE=2][COLOR=#0000ff] public [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]string[/COLOR][/SIZE][SIZE=2] name;[/SIZE]
[SIZE=2][COLOR=#0000ff] public [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]string[/COLOR][/SIZE][SIZE=2] city;[/SIZE]
[SIZE=2][COLOR=#0000ff] public[/COLOR][/SIZE][SIZE=2] Person([/SIZE][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][SIZE=2] age, [/SIZE][SIZE=2][COLOR=#0000ff]string[/COLOR][/SIZE][SIZE=2] name, [/SIZE][SIZE=2][COLOR=#0000ff]string[/COLOR][/SIZE][SIZE=2] city)[/SIZE]
[SIZE=2] {[/SIZE]
[SIZE=2][COLOR=#0000ff] this[/COLOR][/SIZE][SIZE=2].age = age;[/SIZE]
[SIZE=2][COLOR=#0000ff] this[/COLOR][/SIZE][SIZE=2].name = name;[/SIZE]
[SIZE=2][COLOR=#0000ff] this[/COLOR][/SIZE][SIZE=2].city = city;[/SIZE]
[SIZE=2] }[/SIZE]
[SIZE=2][COLOR=#0000ff] public [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][SIZE=2] CompareTo([/SIZE][SIZE=2][COLOR=#2b91af]Person[/COLOR][/SIZE][SIZE=2] other)[/SIZE]
[SIZE=2] {[/SIZE]
[SIZE=2][COLOR=#0000ff] int[/COLOR][/SIZE][SIZE=2] nameSort = [/SIZE][SIZE=2][COLOR=#0000ff]this[/COLOR][/SIZE][SIZE=2].name.CompareTo(other.name);[/SIZE]
[SIZE=2][COLOR=#0000ff] int[/COLOR][/SIZE][SIZE=2] ageSort = [/SIZE][SIZE=2][COLOR=#0000ff]this[/COLOR][/SIZE][SIZE=2].age.CompareTo(other.age);[/SIZE]
[SIZE=2][COLOR=#0000ff] if[/COLOR][/SIZE][SIZE=2] (nameSort == 0)[/SIZE]
[SIZE=2] {[/SIZE]
[SIZE=2][COLOR=#0000ff] return[/COLOR][/SIZE][SIZE=2] ageSort;[/SIZE]
[SIZE=2] }[/SIZE]
[SIZE=2][COLOR=#0000ff] else[/COLOR][/SIZE]
[SIZE=2] {[/SIZE]
[SIZE=2][COLOR=#0000ff] return[/COLOR][/SIZE][SIZE=2] nameSort;[/SIZE]
[SIZE=2] }[/SIZE]
[SIZE=2] }[/SIZE]
[SIZE=2]}[/SIZE]

Then just compare like you have


[SIZE=2][COLOR=#2b91af]Console[/COLOR][/SIZE][SIZE=2].WriteLine([/SIZE][SIZE=2][COLOR=#a31515]"Sorting by age"[/COLOR][/SIZE][SIZE=2]);[/SIZE]
[SIZE=2]lstPersons.Sort([/SIZE][SIZE=2][COLOR=#0000ff]delegate[/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#2b91af]Person[/COLOR][/SIZE][SIZE=2] p1, [/SIZE][SIZE=2][COLOR=#2b91af]Person[/COLOR][/SIZE][SIZE=2] p2) { [/SIZE][SIZE=2][COLOR=#0000ff]return[/COLOR][/SIZE][SIZE=2] p1.CompareTo(p2); });[/SIZE]
[SIZE=2][COLOR=#0000ff]foreach[/COLOR][/SIZE][SIZE=2] ([/SIZE][SIZE=2][COLOR=#2b91af]Person[/COLOR][/SIZE][SIZE=2] p [/SIZE][SIZE=2][COLOR=#0000ff]in[/COLOR][/SIZE][SIZE=2] lstPersons)[/SIZE]
[SIZE=2][COLOR=#2b91af] Console[/COLOR][/SIZE][SIZE=2].WriteLine([/SIZE][SIZE=2][COLOR=#a31515]"Name: {0} - Age: {1} - City: {2}"[/COLOR][/SIZE][SIZE=2], p.name, p.age, p.city);[/SIZE]


Yep, nice and clean

C# Sorting Lists help
(19 posts, started )
FGED GREDG RDFGDR GSFDG