Tuesday, 9 September 2008

Sorting a Generic List in C#

Using Sort algorithm was pretty tedious for asp.net versions prior to 2.0, and also in ASP3.0. But ASP.Net 2.0 onwards we have a very innovative feature of using delegates and sorting out the records in a generic list based on any property of the object in that list.


The following is the code sample for the same.





List SectionsList = SectionsListDataSourceObject;

SectionsList.Sort(

delegate(dataType dt1, dataType dt2)

{

return dt1.SortField.CompareTo(dt2.SortField);

});



You can use all types of fields to sort ranging from string, integer to dates, and you dont have to override any functionality. If you want to sort the records in descending order just reverse the pattern in the return statement. So that would be

return dt2.SortField.CompareTo(dt1.SortField);



Hope this saves someone quite a lot of frustration!

10 comments:

Anonymous said...

歐美潮吹影片歐美模特兒寫真影片欣賞歐美模特兒寫真歐美影集歐美影片歐美整形外科歐美潮吹影片歐美模特兒寫真影片欣賞歐美模特兒寫真歐美影集歐美影片歐美寫真歐美辣妹寫真集歐美貼圖區歐美貼圖片區歐美貼圖歐美絲襪美腿歐美無碼歐美猛男歐美情色貼圖歐美情色歐美素圖歐美素材歐美美女歐美洋妞美女圖片歐美性愛女郎歐美性愛歐美刺青紋身圖片歐美亞語文中心歐美亞歐美男同志歐美免費影片欣賞歐美免費影片歐美免費a片歐美色情影片歐美色情歐美自拍歐美成人影片歐美成人歐美幼幼

Anonymous said...

2013 Hermes HandbagsBirkin Bags qdot hermes bagsKelly Hermes Handbags gqib

Anonymous said...

we have on the above web portal built a huge selection of interesting articles about [url=http://www.rk-37.ru/]средства по уходу за лицом[/url].

Anonymous said...

However, just like clothing, jewelry colors must be tailored to the individual wearer to complement her natural features best.
These simple styles can still include the needed
medical information without being gaudy or
noticeable. Proper care will increase the
life span of your cord.

Here is my homepage; socialzit.com

Anonymous said...

International Fur Association (IFTF) [b][url=http://miumiu.biroudo.jp/#53119]miu miu 財布[/url][/b]
data show that China, Japan and South Korea, fur clothing and accessories sales [b][url=http://prada.ashigaru.jp/#52156]http://prada.ashigaru.jp[/url][/b]
increased more than threefold over the past 10 years, in the 12 months ended April 2012 increased by 5% to $ 5.6 billion. During this period, the global fur sales rose by 3.3% to $ 15.6 billion. "Financial Times" [b][url=http://louisvuitton.biroudo.jp/#524172]ルイヴィトン バッグ 人気[/url][/b]
pointed out that despite the stagnant economic growth, [b][url=http://miumiu.biroudo.jp/#52252]ミュウミュウ バッグ[/url][/b]
but the luxury goods sales performance since the financial crisis relatively well, such as Russia and China economies upstart consumer demand played an important role. [b][url=http://hermes.gamagaeru.jp/#45129]http://hermes.gamagaeru.jp[/url][/b]
International Fur Association, said that in addition to winter, [b][url=http://miumiu.biroudo.jp/#52541]ミュウミュウ 財布[/url][/b]
fashion designers are increasingly turning to the use of fur in the spring and summer clothing design, making fur sales increase. [b][url=http://coach.karamatu.com/#524164]http://coach.karamatu.com/[/url][/b]
The CEO Mark Otten (MarkOaten,), the International Fur Association, said, despite the weak global economy, but he believes [b][url=http://coach.karamatu.com/#524165]コーチ メンズ バッグ[/url][/b]
that the fur sales this year will continue to rise.

Anonymous said...

at the above web -site you can see a wide range of unique news [url=http://apple-televizor.ru/]http://apple-televizor.ru/[/url]

Anonymous said...

A fake watch is never an exact replacement of a designer or branded watch.

They choose brand name designer watches that double as status
symbols. Another watch tipped by some fashionista's is the MK8160.

Here is my web-site ... kekoglobal.tk

Anonymous said...

A fake watch is never an exact replacement of a designer or branded watch.
All Haemmer watches are watertight, resident to
potential damages caused by temperature fluctuations,
and impervious to scratching or cracking from wearer use.
Some economical brands include Invicta watch and Orient watch, and then the
price can reach into the very expensive range depending on
the embellishments or the prestige of a specific brand.


Have a look at my web site; diesel watches

Anonymous said...

A�But in fact, the history of Legos toys dates back many more years.
You'll find that the minifigures from some sets go really well with minifigures from other sets. Included in the furnishings for this area are several books, a telescope, and the Vanishing Cabinet.

Review my website; lego police []

Anonymous said...

buy klonopin online how to buy klonopin online - klonopin 0.5mg