Created
October 25, 2017 19:09
-
-
Save jianminchen/73d61501fa0daf6a1ac38d626c3619d6 to your computer and use it in GitHub Desktop.
C Sharp 2000 things - Item 136 - Sorting an Array of Values Based on an Array of Keys
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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace Item136_Sort | |
{ | |
/// <summary> | |
/// C# 2000 things | |
/// #136 – Sorting an Array of Values Based on an Array of Keys | |
/// https://csharp.2000things.com/2010/10/31/136-sorting-an-array-of-values-based-on-an-array-of-keys/ | |
/// You can use the Array.Sort method to sort two arrays at the same time. The values of one array are | |
/// used as the keys to determine the sort order and the values of the second array are just changed to | |
/// match the sequence of the first array. | |
/// | |
/// Array.Sort takes two arrays. The first is an array of keys, dictating how the elements will be sorted. | |
/// The second will just be reordered to match the sequence of the first array. | |
/// </summary> | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
string[] titles = { | |
"Pride and Prejudice", | |
"Moby-Dick", | |
"A Tale of Two Cities", | |
"Anna Karenina", | |
"Fahrenheit 451" }; | |
string[] firstWords = { | |
"It is a truth universally acknowledged", | |
"Call me Ishmael", | |
"It was the best of times", | |
"All happy families are alike", | |
"It was a pleasure to burn" }; | |
Array.Sort(titles, firstWords); // Sort by title | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment