I have a list of complex objects and would like to order it with linq (to objects) according to an "order-by-keyword":  protected override sealed List&lt;IndexViewModel&gt; Order(List&lt;IndexViewModel&gt; listToOrder, string orderBy) {     List&lt;IndexViewModel&gt; indexViewModels;     switch (orderBy.ToLower())     {         case "creator":             indexViewModels = listToOrder.OrderBy(s =&gt; s.Creator).ToList();             break;         case "startdate":             indexViewModels = listToOrder.OrderBy(s =&gt; s.StartDate).ToList();             break;         ...         default:             indexViewModels = listToOrder.OrderBy(s =&gt; s.CreationDate).ToList();             break;     }      return indexViewModels; }   Is there any way to improve my code? Probably by assigning my keyword to linq and let it decide how to sort it?
11195
I found an example in the  for Dynamic LINQ that allows you to use a sql-like string (e.g. OrderBy("Name, Age DESC")) for ordering. Unfortunately, the method included only works on IQueryable&lt;T&gt;. Is there any way to get this functionality on IEnumerable&lt;T&gt;?
4278
Is there a quick and nice way using linq?
26779
Lets say I have an array like this:  string [] Filelist = ...   I want to create an Linq result where each entry has it's position in the array like this:  var list = from f in Filelist     select new { Index = (something), Filename = f};   Index to be 0 for the 1st item, 1 for the 2nd, etc.  What should I use for the expression Index= ?
28601
I dont know how to sort a list of SObject Case.  I have a List&lt;Case&gt; newCases which i iterate over in for cycle, where i save Cases to my new List&lt;Case&gt; casesList.  for (Case newCase : newCases) {             if(newCase.Case_Content__c == '111' &amp;&amp; newCase.Status == '24'){                 String cuid = newCase.CUID__c;                 cuidSet.add(cuid);                 casesList.add(newCase);                 casesList.sort(); //TODO sorting by CUID__c             }         }   What i need to do is, to sort this new list by field CUID__c. So the Cases with same CUID are together.  Thank you for any help :)
25299
I'm looking for a container that keeps all its items in order.  I looked at SortedList, but that requires a separate key, and does not allow duplicate keys.  I could also just use an unsorted container and explicitly sort it after each insert.  Usage:   Occasional insert Frequent traversal in order Ideally not working with keys separate from the actual object, using a compare function to sort. Stable sorting for equivalent objects is desired, but not required.  Random access is not required.   I realize I can just build myself a balanced tree structure, I was just wondering if the framework already contains such a beast.
121870
I'm using a Query in Google Sheets to pull data from another sheet. That query looks something like this:  =QUERY('Raw Data'!A2:AB,"SELECT E, A, C, D, Y, R, S WHERE S = 'New York'",1 )   What I'd like to do, though, is build a custom sort order within the function so that the Query automatically sorts by rows that have "string1" in column E, followed by those that have "string2" in column E, etc etc. The desired order is not alphabetical so I can't use a clause to sort in ascending or descending order.  I'd also prefer to do this without having to create another sheet where I rank my sort order in a table.
6227
I am looking for a very fast way to filter down a collection in C#.  I am currently using generic List&lt;object&gt; collections, but am open to using other structures if they perform better.  Currently, I am just creating a new List&lt;object&gt; and looping thru the original list.  If the filtering criteria matches, I put a copy into the new list.  Is there a better way to do this?  Is there a way to filter in place so there is no temporary list required?
103121
I am attempting to mock a call to an indexed property. I.e. I would like to moq the following:  object result = myDictionaryCollection["SomeKeyValue"];   and also the setter value  myDictionaryCollection["SomeKeyValue"] = myNewValue;   I am doing this because I need to mock the functionality of a class my app uses.   Does anyone know how to do this with MOQ? I've tried variations on the following:  Dictionary&lt;string, object&gt; MyContainer = new Dictionary&lt;string, object&gt;(); mock.ExpectGet&lt;object&gt;( p =&gt; p[It.IsAny&lt;string&gt;()]).Returns(MyContainer[(string s)]);   But that doesn't compile.  Is what I am trying to achieve possible with MOQ, does anyone have any examples of how I can do this?
62904
I have a List&lt;ListWrapper&gt; comprised of List&lt;Knowledge__kav&gt; and List&lt;Community_Event__c&gt;. I would like to sort List&lt;ListWrapper&gt; by date but the two inner lists have different date fields. How can I sort?  Code Snippets`public class WhatsNewController {  public class ListWrapper{     @auraenabled     Knowledge__kav article {get; set;}     @auraenabled     Community_Event__c event {get; set;} }  public static List&lt;ListWrapper&gt; getWhatsNew(){       //Get all events     String eventQuery = 'Select Id, Title FROM Knowledge__kav WHERE Title != null ';     eventQuery = eventQuery + 'ORDER BY CreatedDate LIMIT 20';     List&lt;Community_Event__c&gt; events = database.query(eventQuery);      //Get all articles with filters (kav) applied     String articleQuery = 'Select Id, Name FROM Community_Event__c ';      articleQuery = articleQuery + 'ORDER BY LastPublishedDate LIMIT 20';     List&lt;Knowledge__kav&gt; articles = database.query(articleQuery);      List&lt;ListWrapper&gt; listEw = new List&lt;ListWrapper&gt;();     for(Community_Event__c cE : events){         ListWrapper eW = new ListWrapper();         eW.event = cE;         listEw.add(eW);         listEw.();                    for(Integer j=0;j&lt;listEw.size();j++)                     System.debug('list values='+ listEw[j]);     }      List&lt;ListWrapper&gt; listAw = new List&lt;ListWrapper&gt;();     for(Knowledge__kav kA : articles){         ListWrapper aW = new ListWrapper();         aW.article = kA;         listAw.add(aW);                    for(Integer j=0;j&lt;listAw.size();j++)                     System.debug('list values='+ listAw[j]);     }      //Sort the lists ----&gt; listEw listAw sort by date and put into following list      List&lt;ListWrapper&gt; listOfWrappers = new List&lt;ListWrapper&gt;();         listOfWrappers.addAll(listAw);         listOfWrappers.addAll(listEw);         listOfWrappers.sort();         //listOfWrappers.Sort((x, y) =&gt; DateTime.Compare(x.Created, y.Created));       return listOfWrappers;  }`
560
It seems that a List object cannot be stored in a List variable in C#, and can't even be explicitly cast that way.  List&lt;string&gt; sl = new List&lt;string&gt;(); List&lt;object&gt; ol; ol = sl;   results in Cannot implicitly convert type System.Collections.Generic.List&lt;string&gt; to System.Collections.Generic.List&lt;object&gt;  And then...  List&lt;string&gt; sl = new List&lt;string&gt;(); List&lt;object&gt; ol; ol = (List&lt;object&gt;)sl;   results in Cannot convert type System.Collections.Generic.List&lt;string&gt; to System.Collections.Generic.List&lt;object&gt;  Of course, you can do it by pulling everything out of the string list and putting it back in one at a time, but it is a rather convoluted solution.
34531
Using LINQ to SQL, I have an Order class with a collection of OrderDetails.  The Order Details has a property called LineTotal which gets Qnty x ItemPrice.   I know how to do a new LINQ query of the database to find the order total, but as I already have the collection of OrderDetails from the DB, is there a simple method to return the sum of the LineTotal directly from the collection?  I'd like to add the order total as a property of my Order class. I imagine I could loop through the collection and calculate the sum with a for each Order.OrderDetail, but I'm guessing there is a better way.
67096
In the absence of an  in a SOQL query, what order will Salesforce return results in?  Clearly if ordering is important then Order By should be specified, but I recently encountered a question about what the order would be if it was undefined.  My initial thoughts were Salesforce would return them in whatever order suited them. Much like .  For simple queries this would typically line up with the creation date descending and the Id ascending. For more complex queries it would depend on the joins being performed.
80985
We all know that a simple statement such as:  SELECT * FROM stuff;   should not produce an ordered result. However, when I try to demonstrate this, it always comes out in primary key order.  Moreover a statement such as:  SELECT thing,whatever FROM stuff GROUP BY thing,whatever;   always appears to sort thing by the last field in the GROUP BY clause, which is not at all helpful.  The question is, under what circumstances does SQL SERVER order the result even if unasked, and what can I do to stop this?  I am trying to demonstrate to my students that order is indeterminate unless specified, but this is not helping my case.  I admit that I am working with a small set of sample data.  Thanks
4045
What is the most efficient way to write the old-school:  StringBuilder sb = new StringBuilder(); if (strings.Count &gt; 0) {     foreach (string s in strings)     {         sb.Append(s + ", ");     }     sb.Remove(sb.Length - 2, 2); } return sb.ToString();   ...in LINQ?
108808
Does a List&lt;T&gt; always guarantee that items will be returned in the order they were added when enumerated?  Updated: Thanks for all the answers folks, puts my mind at ease. I had a quick poke around the List&lt;T&gt; class with .NET Reflector (should've probably done that in the first place) and indeed the underlying store is an array of T (T[]).
70181
I have a table and I am always getting the data by ordering a column. In my case, I  order by datenum. However, the selection is so slow because of the ordering.  Is it possible to order the table after datenum. This column is defined as unique. If  defined as an index, would the selection will be faster?
1054
I am relatively new to Java, and often find that I need to sort a Map&lt;Key, Value&gt; on the values.  Since the values are not unique, I find myself converting the keySet into an array, and sorting that array through array sort with a custom comparator that sorts on the value associated with the key.  Is there an easier way?