Skip to content

Instantly share code, notes, and snippets.

@brunomikoski
Created October 22, 2013 11:06
Show Gist options
  • Save brunomikoski/7098735 to your computer and use it in GitHub Desktop.
Save brunomikoski/7098735 to your computer and use it in GitHub Desktop.
How to sort a array in c#
// Sample sorting by player age:
Array.Sort( arrPlayers, delegate(Player a, Player b)
{
if( a.Age > b.Age ) // if a is higher than b
return 1;
else if ( a.Age < b.Age ) // if b is higher than a
return -1;
return 0; // if both are equals return 0
});
// Another sample:
Array.Sort( arrInts, delegate(int a, int b)
{
if( a > b ) // if a is higher than b
return 1;
else if ( a < b ) // if b is higher than a
return -1;
return 0; // if both are equals return 0
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment