site stats

C# order by another list

WebApr 7, 2024 · Innovation Insider Newsletter. Catch up on the latest tech innovations that are changing the world, including IoT, 5G, the latest about phones, security, smart cities, AI, robotics, and more.

c# - Linq OrderBy against specific values - Stack Overflow

WebOct 14, 2014 · You can join both lists and order by IndexOf var ordered = (from ds in dataToSort join d in data on ds.ID equals d.ID orderby data.IndexOf (d) select ds).ToList (); Share Improve this answer Follow edited Apr 24, 2012 at 10:50 answered Apr 24, 2012 at 10:33 Tim Schmelter 444k 72 677 929 Add a comment 0 This should do the trick: WebSep 15, 2024 · The next example demonstrates how to use the orderby descending clause in a LINQ query to perform a primary sort, in ascending order, and a secondary sort, in descending order. The strings are sorted primarily by length and secondarily by the first letter of the string. formwise washrooms wellingborough https://balverstrading.com

c# - Sort a list from another list IDs - Stack Overflow

Webvar hold = MyList.OrderBy (x => x.StartDate) .ThenByDescending (x => x.EndDate) .ToList (); You can also use query syntax and say: var hold = (from x in MyList orderby x.StartDate, x.EndDate descending select x).ToList (); ThenByDescending is an extension method on IOrderedEnumerable which is what is returned by OrderBy. WebList data = new List { "A","B","A","C","B","C","D","E" }; List preferences = new List { "A","B","C" }; IEnumerable orderedData = data.OrderBy ( item => preferences.IndexOf (item)); This will put all items not appearing in preferences in front because IndexOf () returns -1. WebDec 29, 2024 · Feb 15, 2012. Posts: 1,726. I think the idea is that is that if you absolutely need to sort parallel arrays (2 arrays where A and B have different info about the same item, so must always have identical indexes), you can copy any sorting code. Then where-ever it says swap (A, [A [j]), add swap (B,B [j]) right after it. form wisuda unila

ChatGPT cheat sheet: Complete guide for 2024

Category:ChatGPT cheat sheet: Complete guide for 2024

Tags:C# order by another list

C# order by another list

Enumerable.OrderBy Method (System.Linq) Microsoft Learn

WebJan 29, 2016 · C#: Sort one collection based on another one. In order to sort a collection of objects based on another list that contains the proper order, we can create an extension method that performs a `Join` between these two collections and pass a `func` as innerKeySelector argument to allow us to sort this collection based on whatever key we … WebNov 16, 2016 · 3 Answers Sorted by: 8 You can use Linq to order your users: using System.Linq; mylist.OrderBy (x => x.LastName); If you have same last names you can order users by middle name. If you have same middle names you can order users by first name: mylist.OrderBy (x => x.LastName).ThenBy (x => x.MiddleName).ThenBy (x => …

C# order by another list

Did you know?

WebThe OrderBy () method returns a collection of items as opposed to ordering in-place, so you would need to set your Cars to that specific value: entities.ForEach (h => { // This will order your cars for this person by Make h.Cars = h.Cars.OrderBy (t => t.Make).ToList (); }); WebApr 30, 2010 · Daniel is nearly right, but an easy way to use the order of the incoming list is to order by the index of the ID in that list: public List GetMyTypes (List ids) { return (from myType in db.MyTypes where ids.Contains (myType.Id) orderby ids.IndexOf (myType.Id) select myType).ToList (); }

WebJun 27, 2011 · 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 => dict … WebMar 12, 2014 · OrderBy has similar flexibility, but returns a new sequence which is sorted rather than modifying the original list. So, you could do: var newList = modelList.OrderBy (m => m.name).ToList (); // descending var newList = modelList.OrderByDescending (m => m.name).ToList ();

WebJul 12, 2024 · A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions. WebFeb 23, 2024 · If possible values range of Letter is much smaller than listA, then better consturct orders like this: foreach (string letter in listB.Select (x=>x.Letter)) { orders.Add (letter, listA.IndexOf (letter)); } Update If letters ' range are out of listA, and since OP want the out ones to be at the end:

WebJul 22, 2010 · 1111. If you need to sort the list in-place then you can use the Sort method, passing a Comparison delegate: objListOrder.Sort ( (x, y) => x.OrderDate.CompareTo (y.OrderDate)); If you prefer to create a new, sorted sequence rather than sort in-place then you can use LINQ's OrderBy method, as mentioned in the other answers.

WebMar 6, 2013 · Morover, I have another list of items, which are represented by the ids described above. List docs = GetDocsFromDb(...) I need to keep the same order in both collections, so that the items in List must be in the same position than in the … digging a ditch dave matthewsWebJan 2, 2024 · Sort and merge list based on another list. Here I'm merging a list to end of another list after sorting it according to another list.This should sort processdetails list … digging a ditch with a tractorWebApr 7, 2024 · Innovation Insider Newsletter. Catch up on the latest tech innovations that are changing the world, including IoT, 5G, the latest about phones, security, smart cities, AI, … digging activitiesWebJan 22, 2013 · Here's a nifty trick; basically, a Dictionary can be thought of as a List of KeyValuePair<> objects. Dictionary myDictionary = new Dictionary (); foreach (var item in myDictionary) // item is a KeyValuePair, enumerated in order of their insertion into the dictionary. Now that it is in a list, it's easy to ... digging a ditch with a backhoeWebThe Linq OrderBy method in C# is used to sort the data in Ascending Order. The most important point that you need to keep in mind is that this method is not going to change the data rather it is just going to change … digging a drain trenchWebThis method performs a stable sort; that is, if the keys of two elements are equal, the order of the elements is preserved. In contrast, an unstable sort does not preserve the order of elements that have the same key. In query expression syntax, an orderby (Visual C#) or Order By (Visual Basic) clause translates to an invocation of OrderBy. form with checkboxWebJan 2, 2024 · 4 Here I'm merging a list to end of another list after sorting it according to another list.This should sort processdetails list according to the order in routeplans list if any item not present in processdetails list is found it should keep the order and add it to processdetails list. digging a dry well for drainage