site stats

C# order list by another list

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 (); } WebOct 5, 2014 · 89 Similar to List<> OrderBy Alphabetical Order, we want to sort by one element, then another. we want to achieve the functional equivalent of SELECT * from Table ORDER BY x, y We have a class that contains a number of sorting functions, and we have no issues sorting by one element. For example:

c# - Sort list of strings by another list of strings - Stack Overflow

WebJan 2, 2024 · 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 … WebMay 5, 2011 · I have a list of MyFirstObject called AList. MyFirstObject class has two properties: ID, Name. I have another list of MySecondObject called BList. MySecondObject only has a property:ID. AList is ordered by its ID property. BList is randomly ordered and not by its ID property. We want to achieve the order of BList in AList. (Apologies for the ... puffin tv browser subscription https://boudrotrodgers.com

c# - How to order a List by the order of another List? - Stack Overflow

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. WebMay 24, 2012 · 1. Try using some linq. List itm = new List; //Fill itm with data //get selected item from control string selectedcategory = cboCatetories.SelectedItem; var itms = from BO in itm where itm.ItemCategory = selectedcategory select itm; itms now contains all items in that category. Share. WebFeb 23, 2024 · Sorted by: 2 Assume value of Letter are within listA. It's worth considering redesigning the algorithm if the assumption is false. And it may depend on requirement of expected order the out of range ones. var sortedSamples = listB.OrderBy (x=>listA.IndexOf (x.Letter)); For better performance, cache the orders into a dictionary: puffin \u0026 oyster

c# - How to order a List by the order of another List? - Stack Overflow

Category:linq - Filter a list by another list C# - Stack Overflow

Tags:C# order list by another list

C# order list by another list

C# sort list in alphabetical order - Stack Overflow

WebJul 3, 2024 · I'm using C# on Framework 3.5. I'm looking to quickly sort a Generic List. For the sake of this example, let's say I have a List of a Person type with a property of lastname. ... List OrderBy Alphabetical Order. Ask Question Asked 14 years, 6 months ago. Modified 7 months ago. Viewed 773k times 481 I'm using C# on Framework 3.5. I'm ... WebFeb 26, 2015 · List result = list.OrderBy (str => { int index = comparer.IndexOf (str); return index == -1 ? int.MaxValue : index; }).ToList (); Here is a long version which might be more readable for some. It's also more flexible since you can specify what happens with the not-contained items.

C# order list by another list

Did you know?

WebJul 22, 2010 · Simplest way to order a list is to use OrderBy List objListOrder = source.OrderBy (order => order.OrderDate).ToList (); If you want to order by multiple columns like following SQL Query. ORDER BY OrderDate, OrderId To achieve this you can use ThenBy like following. WebMar 12, 2014 · Add a comment. 1. Step 1: Implement IComparer interface. public class SortByName : IComparer { public int Compare (Customer x, Customer y) { return x.Name.CompareTo (y.Name); } } Step 2: Pass an instance of the class that implements IComparer interface, as an argument to the Sort () method. SortByName …

WebMar 18, 2024 · //Comparing list // The below list contains Ids but not in a different order compared to the above list IList Ids = new List(); Ids.Add(12); … 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.

WebNov 3, 2013 · You are confusing LINQ operations with a method that changes the variable it is applied to (i.e. an instance method of the object). LINQ operations (i.e. the .OrderBy) returns a query.It does not perform the operation on your object (i.e. lst).. You need to assign the result of that query back to your variable:. lst = lst.OrderBy(p => p).ToList(); WebNov 20, 2024 · Using extension methods, first OrderBy the enum, ThenBy name. var sorted = list.OrderBy ( m => m.Sort ).ThenBy ( m => m.Name ); Share Improve this answer Follow answered Mar 10, 2010 at 2:59 tvanfosson 521k 98 699 794 Add a comment 5 Aside from the nice LINQ solutions, you can also do this with a compare method like you mentioned.

WebFeb 22, 2012 · A "Thing" has a unique name but no inherent value to order by. However, the list of selected "Things" needs to be ordered by the user. This order must be able to be edited and persisted. The current implementation does not take order into account. It uses 2 List objects - one for all Thing's and another for selected Thing's. seattle direct flight to europeWebApr 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. puffinus auricularis newelliWebJan 29, 2016 · 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 want. [gist id=”c1b6681e3e04c0d1d928″] Example puffin tv浏览器 apkWebMar 7, 2016 · 1 Answer. You can do select the index of the item, throw it into an anonymous object order by that and then select the string again : var res = kws .Select (a=>new {orderOf=order.IndexOf (a.fkp_keyword),val=a}) .OrderBy (a=>a.orderOf) .Select (a=>a.val) .ToList (); If you have items that are not in the list you are ordering by then they will be ... seattle directoryWebApr 2, 2013 · c# - Find items from a list which exist in another list - Stack Overflow Find items from a list which exist in another list Ask Question Asked 10 years ago Modified 2 years, 7 months ago Viewed 93k times 53 I have a List PropA { int a; int b; } and another List PropX { int a; int b; } puffin vape shop coupon codeWebJun 27, 2011 · Any idea how I could get the data list to be sorted to be in this order, using the dynamic sort statement: Apple, Andy Apple, Amy Banana, Bob Coconut, Charlie Using .NET 4.0 if some fancy LINQ will work. Thanks for any suggestions. puffinus baroliWebApr 7, 2016 · I would like my first list to be ordered like this: A, B, C, D, X, W. Actually the last two letters X and W doesn't matter how they are ordered, but they should be at the … puffin\u0027 billy melodi light orchestra