score:0
i would create a class to hold your information
public class namedate
{
private string name;
private datetime date;
public string name
{
get { return name; }
set { name = value; }
}
public datetime date
{
get { return date; }
set { date = value; }
}
}
i would then populate a list<namedate>
to hold your items. when this is done, to sort the items you can use linq
list<namedate> somelist = new list<namedate>();
somelist = getnamedatelist();
var orderedbynamelist = somelist.orderby(e => e.name);
var orderedbydatetimelist = somelist.orderby(e => e.date);
i hope this helps.
score:4
something like this:
yourlist.orderby(o => o.datetime).thenby(o => o.number);
score:4
first you create two functions:
var sortbydate=(p=>p.date);
var sortbyname=(p=>p.name);
then you have, for example, a list containing the two
var sortdimensions=new list<object>({sortbydate,sortbyname});
then you if you want to sort by first date then name you do:
mylist.orderby(sortdimensions[0]).thenby(sortdimensions[1]);
now, here is the tricky part, if you want to sort by name first then date you just alter the order of the functions in the sortdimensionslist:
sortdimensions.remove(sortbyname);
sortdimensions.insert(0,sortbyname);
this way, the same orderby statement will give you a different result.
ps. admittedly, this is rather close to being pseudocode since you may have to use something other than list<object>
for the sortdimensions collection to avoid compiler errors, but i don't have access to an ide at the moment, but the spirit of the answer remains the same. create a collection, store delegate functions in it, then alter the order within the collection to achieve different sorting dimensions using the same generic code.
score:6
Source: stackoverflow.com
Related Query
- Sorting a collection by multiple fields
- Order collection of items with multiple date fields
- LINQ to Entites/IQueryable: Sorting on multiple fields
- In C#, how can I sort a collection of objects by multiple fields of that object?
- Enumerable OrderBy used to order collection by multiple optional fields
- How to do joins in LINQ on multiple fields in single join
- Select Multiple Fields from List in Linq
- How to use LINQ Distinct() with multiple fields
- LINQ Group By Multiple fields -Syntax help
- Select multiple fields group by and sum
- Filter/Search using Multiple Fields - ASP.NET MVC
- Sorting an observable collection with linq
- Linq Group on Multiple Fields - VB.NET, Anonymous, Key
- Get new indices of items in a collection after sorting using LINQ
- How can I order by multiple fields when using Linq.Dynamic?
- This code returns distinct values. However, what I want is to return a strongly typed collection as opposed to an anonymous type
- Remove duplicates from list based on multiple fields or columns
- Linq group by multiple fields across different tables
- Does this LINQ code perform multiple lookups on the original data?
- LINQ WHERE method alters source collection
- How to combine multiple fields into one field using LINQ
- How to do left joins in LINQ on multiple fields in single join
- How to reuse a linq expression for 'Where' when using multiple source tables
- build an expression with multiple sorting
- Build GroupBy expression tree with multiple fields
- The data reader has more than one field. Multiple fields are not valid for EDM primitive types
- LINQ string[] against multiple fields
- LINQ Source Code Available
- Conditional Multiple Fields Searching and Filtering in LINQ
- multiple orderby in this linq code
More Query from same tag
- C# Linq query to find strings in an array that end with strings in another array
- How can I display foreign key data in a datagrid
- LINQ .ToListAsync failing when using two tables
- "Cannot call methods on DateTime", and other limitations
- LINQ to SQL inserting large object from .NET
- How can I get a List from all nodes in a tree using LINQ?
- BLToolkit custom data provider
- Linq-to-Sql: recursively get children
- Excluding Condition if Parameter is null
- ForEach giving Invalid Cast at Runtime
- A type that implements IEnumerable 'System.Collections.Generic.List`1' cannot be initialized in a LINQ to Entities query
- How to merge a list of lists with same type of items to a single list of items?
- ILookup store item under multiple keys
- Find intersecting DataRows in a List of DataTables
- Cannot implicitly convert type 'System.Collections.Generic.IEnumerable>>' to 'System.Collections.Generic.IEnumerable'. An explicit conversion exists
- .NET Linq Left Join
- LINQ - many to many
- How to match all objects from two lists into pairs of two based on proximity of DateTime values
- C#, How to make function that order list as we need in parameter
- Convert SQL query with an "In" condition to Lambda expression
- how to extract a negative symbol from text
- Order by date diff in LINQ
- Gridview sorting not working with Linq to SQL
- What does the # sign in AnonymousType0#1`6 mean?
- linq-to-sql InsertOnSubmit
- How to filter a Linq Query from XML to IEnumerable of Objects
- populate object where it contains values from another list
- How do I retrieve values from an XML that (I think) doesn't have standard formatting?
- 'The LINQ expression node type 'Invoke' is not supported in LINQ to Entities' when lambda is passed as a parameter, but not when used directly
- Check if one IEnumerable contains all elements of another IEnumerable