Skip to content

Instantly share code, notes, and snippets.

@ppolyzos
Last active May 21, 2021 13:43
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ppolyzos/c1b6681e3e04c0d1d928 to your computer and use it in GitHub Desktop.
Save ppolyzos/c1b6681e3e04c0d1d928 to your computer and use it in GitHub Desktop.
C#: Sort one collection based on another
/// <summary>
/// Sort one collection based on keys defined in another
/// </summary>
/// <returns>Items sorted</returns>
public static IEnumerable<TResult> SortBy<TResult, TKey>(
this IEnumerable<TResult> itemsToSort,
IEnumerable<TKey> sortKeys,
Func<TResult, TKey> matchFunc)
{
return sortKeys.Join(itemsToSort,
key => key,
matchFunc,
(key, iitem) => iitem);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment