Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

@divega 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