Skip to content

Instantly share code, notes, and snippets.

@bashkirtsevich
Created February 10, 2017 12:19
Show Gist options
  • Select an option

  • Save bashkirtsevich/03d0b0a89b5362c4adb52e76ad4293ec to your computer and use it in GitHub Desktop.

Select an option

Save bashkirtsevich/03d0b0a89b5362c4adb52e76ad4293ec to your computer and use it in GitHub Desktop.
C# sorting
// 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