James
James

Reputation: 2999

.net mvc3 sort list of objects by date variable

I have a list of "BlogPost" each with a date variable.

Is there an easy way in .net mvc3 to sort the list based on their date or do I need to write my own sorting alogorithm by hand?

Upvotes: 0

Views: 918

Answers (2)

John x
John x

Reputation: 4031

or do I need to write my own sorting alogorithm by hand?

NO!! we dont live in the stone age anymore...

you have provided no information in your question such as what is the data access mechanism Linq? using any ORM's like NHibernate? or entity? or linq2sql? or querying the db by opening a connection and then executing sql statements?

if you are using Linq then you can try

var _x = ( from x in DBContext.BlogPost
           orderby x.Date select x).ToList();

or

string myConnectionString = "connectionstring"; //you connectionstring goes here

SqlCommand cmd= new SqlCommand("select * from BlogPost order by Date", new SqlConnection(myConnectionString));
cmd.Connection.Open();
//some execute scalar stuff goes here i really dont remember 
cmd.Connection.Close();

Upvotes: 1

Gabe
Gabe

Reputation: 50493

Making a lot of assumptions since you gave very little information, just use Linq.

List<BlogPost> posts = GetBlogPosts();
posts.Sort(b => b.Date);

Upvotes: 2

Related Questions