score:7
Try something like this:
var partiallySorted = list.Where(x => x < 11)
.Concat(list.Where(x => x >= 11 && x <=15).OrderBy(/*blah*/)))
.Concat(list.Where(x => x > 15));
score:2
Simply get the required range based on some criteria and apply the sort on the resultant range using Linq.
List<int> numbers = new List<int>() { 15, 4, 1, 3, 2, 11, 7, 6, 12, 13 };
var range = numbers.Skip(3).Take(4).OrderBy(n => n).Select(s => s);
// output: 2, 3, 7, 11
score:4
List<int> list = new List<int>() {1,2,3,15,11,13,10,6,7};
list.Sort(3, 4,Comparer<int>.Default);
score:0
No, the Linq extension methods will never modify the underlying list. You can use the method List<T>.Sort(int index, int counter, IComparer<T> comparer)
to do an in-place sort:
var list = new List<int> {1, 2, 3, 15, 11, 13, 10, 6, 7};
list.Sort(3, 4, null);
score:1
Use this for default inline List Sort:
Syntax: List.Sort(start index, number of elements, Default Comparer)
List<int> numbers = new List<int> { 1, 2, 3, 15, 11, 13, 10, 6, 7 };
numbers.Sort(3, 6, Comparer<int>.Default);
If you want to sort by [properties/attributes] of the element or precisely something else use the below method,
I had sorted the string by number of characters, and also from 2nd element to end of List.
Syntax: List.Sort(start index, number of elements, Custom Comparer)
List<string> str = new List<string> { "123", "123456789", "12", "1234567" };
str.Sort(1, str.Count - 1, Comparer<string>.Create((x, y) => x.Length.CompareTo(y.Length)));
Source: stackoverflow.com
Related Articles
- Sorting range of list using LINQ
- How to select values within a provided index range from a List using LINQ
- Populate a list with a specific range of numbers by using LINQ
- Using Linq to select a range of members in a list
- Using Linq find first object in list sorting by property A, then property B
- sorting list of objects with null properties using linq
- Adding a range of values in to a List using LinQ
- Sorting a list based on status using linq c#
- Sorting Using Linq Where list contains alphabets and Numerics
- Convert an integer range to a list of strings using LINQ
- Compiling Error with LINQ Sorting Code Using List<T>
- sorting list of objects by 2 property of that objects using linq
- Sorting list of strings using linq with custom sort
- Sorting list of Objects using LINQ and private IComparer?
- Construct a list of wpf Hyperlink elements from an XML source file using Linq
- How to get list of record within a range using LINQ
- c# Linq or code to extract groups from a single list of source data
- Updating List using LINQ working when execute from Immediate window, not from code direct
- Getting InvalidCastException when trying to implement sorting in Entity Framework Code First using Linq
- Adding range to list using linq in C#
- How to find a range matching certain conditions in a list using LINQ
- Remove duplicates in the list using linq
- Convert string[] to int[] in one line of code using LINQ
- Sorting a list using Lambda/Linq to objects
- Searching if value exists in a list of objects using Linq
- Convert list to dictionary using linq and not worrying about duplicates
- Using Linq to group a list of objects into a new grouped list of list of objects
- Using LINQ to Update A Property in a List of Entities
- Return list using select new in LINQ
- Return list of specific property of object using linq
- Query a subset of an entity and cast it into another model using EF 6
- select object which matches with my condition using linq
- C# Method on one array (values) conditional on a second array (dates)
- Select multiple columns with linq to sql
- How to loop over System.Collections.Generic.List`1[CardDistro.Models.Transaction]
- How to solve error in LINQ: Data is null or empty
- Custom jQuery Selector That Returns Selected Element Properties Like LINQ
- Populating nested dictionary from database query c#
- Refactoring predicate out of the Lambda expressoin caueses an exception
- LINQ to XML: How to select the next element
- Order of groups with dynamic linq
- Calculate difference from previous item with LINQ
- Dictionary group by value
- How Build Lambda Expression Tree with multiple conditions
- When to use BlockingCollection and when ConcurrentBag instead of List<T>?
- Check if a query contains things out of the list
- Packing items from the List into a List of pairs
- How to Linq through DataGrid SelectedItems in WPF?
- Is LINQ to SQL the best way to build a Model or create my own classes
- Quick Question: C# Linq "Single" statement vs "for" loop