Skip to content

Instantly share code, notes, and snippets.

@uchida
Created November 28, 2011 13:59
Show Gist options
  • Save uchida/1400497 to your computer and use it in GitHub Desktop.
Save uchida/1400497 to your computer and use it in GitHub Desktop.
C# sample code for index based sort
// Guffa's answer in http://stackoverflow.com/questions/659866/is-there-c-sharp-support-for-an-index-based-sort
using System;
using System.Collections.Generic;
using System.Linq;
namespace IndexSortTest {
class Program {
static void Main(string[] args) {
List<int> list = new List<int> {4, 2, 3, 1, 8};
int N = list.Count;
int[] index = Enumerable.Range(0, N).ToArray<int>();
Array.Sort<int>(index, (a, b) => list[a].CompareTo(list[b]));
foreach (int i in index) {
Console.WriteLine("{0}: {1}", i, list[i]);
}
Console.ReadLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment