c# - Order Dictionary from Keys from another Dictionary -
i have 2 dictionaries:
dictionary<string, tuple<t, t>> dict1 dictionary<enum, tuple<string, datetime, datetime>> dict2
the string value in first item in tuple equal key in first dictionary. sort second dictionary enum value , sort first dictionary based on order dict2 has. how able while retaining other keys might in dict1? i'm able far sorting dictionary.
var positions = new dictionary<enum, string>(); //this foreach loop can simplified linq expression. foreach (var position in dict2) { var isfound = dict1.any(x => x.key == position.value.item1.value); if (isfound) { clubpositions.add(position.key, position.value.item1.value); } } var sortedpositions = positions.orderbydescending(x => x.key);
the way understand question not sort first dictionary, able iterate elements in order defined second dictionary keys. if that's correct, following should trick:
var orderedkeys = new hashset<string>(dict2.orderby(e => e.key).select(e => e.value.item1)); var orderedentries = orderedkeys.where(dict1.containskey) .select(key => new keyvaluepair<string, tuple<t, t>>(key, dict1[key])) .concat(dict1.where(e => !orderedkeys.contains(e.key)));
note put entries has no corresponding key last in order.
Comments
Post a Comment