Created
February 10, 2017 12:19
-
-
Save bashkirtsevich/03d0b0a89b5362c4adb52e76ad4293ec to your computer and use it in GitHub Desktop.
C# sorting
This file contains hidden or 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
| // array of custom type | |
| User[] users = new User[3] { new User("Betty", 23), // name, age | |
| new User("Susan", 20), | |
| new User("Lisa", 25) }; | |
| // sort array by name | |
| Array.Sort(users, delegate(User user1, User user2) { | |
| return user1.Name.CompareTo(user2.Name); | |
| }); | |
| // write array (output: Betty23 Lisa25 Susan20) | |
| foreach (User user in users) Console.Write(user.Name + user.Age + " "); | |
| // sort array by age | |
| Array.Sort(users, delegate(User user1, User user2) { | |
| return user1.Age.CompareTo(user2.Age); // (user1.Age - user2.Age) | |
| }); | |
| // write array (output: Susan20 Betty23 Lisa25) | |
| foreach (User user in users) Console.Write(user.Name + user.Age + " "); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment