Sort List of Dictionaries?

Private static IEnumerable> Sort(IEnumerable> data, string orderByString) { var orderBy = orderByString. Split(','). Select( s => s.

Split(new {' '}, StringSplitOptions. RemoveEmptyEntries)) . Select (a => new {Field=a0,Descending = "desc".

Equals (a1, StringComparison. InvariantCultureIgnoreCase)}) . ToList (); if (orderBy.

Count == 0) return data; // First one is OrderBy or OrderByDescending. IOrderedEnumerable> ordered = orderBy0. Descending?Data.

OrderByDescending (d => dorderBy0. Field) : data. OrderBy (d => dorderBy0.

Field); for (int I = 1; I dorderClause. Field) : ordered. ThenBy(d => dorderClause.

Field); } return ordered; }.

Mmm that's not very dynamic – sehe Jun 27 at 23:11 @CmdrTallen, new answer. – agent-j Jun 27 at 23:26 a bit more dynamic already. Good point about case insensitive comparison - I'll nick that from you :) – sehe Jun 27 at 23:27 Worked just as I needed it, thanks!

– CmdrTallen Jun 270 at 14:01.

Data. OrderBy(dict => dict"Lastname") . ThenByDescending(dict => dict"Firstname") If it's dynamic: var sorted = data.

OrderBy(item => 1); // identity (stable) sort orderby = "Lastname asc, Firstname desc"; foreach (var key in orderby. Split(','). Select(clause => clause.Trim())) { if (key.

EndsWith(" desc", StringComparison. CurrentCultureIgnoreCase)) { key = key. Substr(0, key.

Length - 5); sorted = data. ThenByDescending(dict => dictkey); } else { if (key. EndsWith(" asc", StringComparison.

CurrentCultureIgnoreCase)) { key = key. Substr(0, key. Length - 4); } sorted = data.

ThenBy(dict => dictkey); } }.

Added some sample code (without error checking! ) that allows a dynamic orderby specification with (optional) asc or desc modifiers. No compiler ever saw this code, so it may contain silly typo's – sehe Jun 27 at 23:08 +1 for beating me to the answer... I started out a little more complicated than it needed to be.

– agent-j Jun 27 at 23:28.

I'm not sure if you have the best implementation here, but as the above is an example and not a real world case, here you go: data . OrderBy(x => x"Lastname") . ThenByDescending(x => x"Firstname") .

Select(x => new { Lastname = x"Lastname", Firstname = x"Firstname", }).

Unless there is a specific reason you need to use Dictionaries, for which I can't think, something similiar to this would be better; public class Person { public string FirstName {get; set;} public string LastName {get; set;} } var people = new { new Person { FirstName = "Amy", LastName = "Apple" }, new Person { FirstName = "Andy", LastName = "Apple" }, new Person { FirstName = "Charlie", LastName = "Coconut" } }; var sortedPeople = people . OrderBy(f => f. LastName) .

ThenByDescending(f => f. FirstName); Updated based on comments; See this post for a possible solution; Entity Framework 4.1 simple dynamic expression for object. Property = value and Dynamic linq query with multiple/unknown criteria (the above has now been fully implemented as below) public static int UpdateSegment(int segmentId) { Table Contacts; var conditionsFormatted = new Dictionary(); //Retrieve all conditions var segmentConditions = Lists.

GetSegmentConditions(segmentId); //Iterate through conditions and process them foreach (var condition in segmentConditions) { switch (condition. Operator) { case SegmentCondition.OperatorType. Equals: condition.

Condition = string. Format("{1}=\"{0}\"", condition. Criteria, condition.

Field); break; case SegmentCondition.OperatorType. Contains: condition. Condition = string.

Format("{1}. Contains(\"{0}\")", condition. Criteria, condition.

Field); break; default: throw new ApplicationException("Unexpected Operator for Condition"); } } var db = new DbContext(ConfigurationManager. ConnectionStrings"c". ConnectionString); var statusConditions = "Status = 1"; var results = (IQueryable)db.Contacts.

Where(statusConditions); var distinctFields = (from c in segmentConditions select c. Field).Distinct(); foreach (var distinctField in distinctFields) { var values = (from s in segmentConditions where s. Field == distinctField select s.

Condition).ToArray(); var valuesJoined = string. Join("||", values); results = results. Where(valuesJoined); } results = results.

Select("id"); var xml = new StringBuilder(); xml. Append(""); foreach (var id in results) { xml. Append(String.

Format("{0}", id)); } xml. Append(""); var idXml = XDocument. Parse(xml.ToString()); return Lists.

UpdateSegmentContacts(idXml.ToString(), segmentId); } Might help, then again, might not!

Nice addition, but still not dynamic; If you use non-anonymous types like this, DLINQ might apply – sehe Jun 27 at 23:13 ah yes, missed that point! – ChrisBint Jun 27 at 23:17 woah... entity framework, database connection strings, tables, linq to XML, system overload! This gotta be something very complex; but I can't see the forest anymore :) Then again, perhaps the OP is getting it's data from a similar source and it might not be be all that... erm... convoluted as it might seem at first glance?

– sehe Jun 27 at 23:32 PS. Prefer .AsQueryable() extension to the (IQueryable) cast; the extension allows better expression derivation and more compile time checks – sehe Jun 27 at 23:33 indeed, but it solved a specific problem very well. Will refactor what I can when I get onto the technical debt backlog.

Tests go green for now! – ChrisBint Jun 27 at 23:34.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions