Skip to content

Instantly share code, notes, and snippets.

@divega
Created November 7, 2019 08:24
Show Gist options
  • Save divega/f0e9a22547c5f91ee6d581fca3e06e05 to your computer and use it in GitHub Desktop.
Save divega/f0e9a22547c5f91ee6d581fca3e06e05 to your computer and use it in GitHub Desktop.
Sorting an array with the order of another array using LINQ's Join
using System;
using System.Linq;
namespace SortWithJoin
{
class Program
{
static void Main(string[] args)
{
var keys = new [] { 5, 7, 3, 2, 1, 4, 6, 9, 8 };
var docs = new[] { (key: 1, val: "uno"), (key: 2, val: "dos"), (key:3, val:"tres") };
var sorted = from key in keys
join doc in docs on key equals doc.key
select doc;
foreach(var doc in sorted)
{
Console.WriteLine(doc);
}
}
}
}
@divega
Copy link
Author

divega commented Nov 7, 2019

Output is

(3, tres)
(2, dos)
(1, uno)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment