Reputation: 13
I have an IList:
IList[] info = new IList[2];
info[0] = Date; //List<DateTime> item
info[1] = something; //List<double> item
So, I want sort info
by dates in info[0]
. Like that:
Date.Sort((a, b) => a.CompareTo(b));
But when i'm trying to do it:
IEnumerable[] sortedEnum = info.OrderBy(info[0].Sort((a, b) => a.CompareTo(b)));
I get:
'System.Collections.IList' does not contain a definition for 'Sort' and no extension method 'Sort' accepting a first argument of type 'System.Collections.IList' could be found (are you missing a using directive or an assembly reference?).
Can someoene help me with this?
Upvotes: 0
Views: 762
Reputation: 103575
Building on Marc's
List<(DateTime when, double value) list = info[0].Zip(info[1], (d,v)=> (d,v));
And as Magnetron points out, if you don't have C#7 yet,
var list = info[0].Zip(info[1], (d,v)=> Tuple.Create(d,v));
list.Sort((x,y) => x.Item1.compareTo(y.Item1));
Upvotes: 0
Reputation: 1064044
I think this is an X/Y problem. You're trying to solve a complex "sort two lists congruent to each-other". And ... you can do that, but it is very hard. Array.Sort
has a suitable method for sorting two arrays in connection, but: not lists. You could try and do that, but frankly I think you're solving the wrong problem. If each DateTime
and double
is logically connected, then rather than having two lists: have one list with a composite value. For example:
var list = new List<(DateTime when, double value)>();
list.Add((dateX, valueX)); // your values...
list.Add((dateY, valueY));
list.Add((dateZ, valueZ));
list.Sort((x,y) => x.when.compareTo(y.when));
Here I'm using tuple-types, but the same thing can be done by declaring your own type to hold the composite values. You might also want to look into pre-sorted list container types, but that places more demands on the key value (uniquity, etc).
Upvotes: 2