You'll have to use this overload and provide a custom IComparer<T>
. The relatively new Comparer<T>.Create
method makes this a lot easier because you can simply turn a delegate or lambda expression into an IComparer<T>
without having to code a full implementation yourself. It's not clear from the question what the datatype of Weights
and Values
are, but here's an example using double[]
and int[]
respectively:
var Weights = new [] { 1.7, 2.4, 9.1, 2.1, };
var Values = new [] { 7, 9, 5, 3, };
Array.Sort(Weights, Values, Comparer<double>.Create((x, y) => y.CompareTo(x)));
And just for fun, here's a solution using LINQ:
var pairs = Weights.Zip(Values, Tuple.Create);
var orderedPairs = pairs.OrderByDescending(x => x.Item1);
I'd also recommend that you consider using a class to store weights and values together rather than as two separate arrays.