Created
October 22, 2013 11:06
-
-
Save brunomikoski/7098735 to your computer and use it in GitHub Desktop.
How to sort a array in c#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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