Computer >> Máy Tính >  >> Lập trình >> C#

C # theo thứ tự từ khóa

Sử dụng từ vựng theo thứ tự để sắp xếp danh sách theo thứ tự tăng dần hoặc giảm dần.

Sau đây là danh sách -

 List  myList =new List  (); myList.Add ("xe tải"); myList.Add ("xe buýt"); myList.Add ("xe đạp"); myList.Add ("xe máy" "); 

Bây giờ chúng ta hãy sắp xếp danh sách theo thứ tự giảm dần -

 myLen =from phần tử trong phần tử myListorderby. 

Đây là mã hoàn chỉnh -

Ví dụ

 using System; using System.Collections.Generic; using System.Linq; class Demo {static void Main () {List  myList =new List  (); myList.Add ("xe tải"); myList.Add ("xe buýt"); myList.Add ("xe đạp"); myList.Add ("xe máy"); var myLen =from element trong myList orderby element.Length select element; Console.WriteLine ("Thứ tự tăng dần ..."); foreach (string str trong myLen) {Console.WriteLine (str); } myLen =from element trong myList orderby element.Length select element giảm dần; Console.WriteLine ("Thứ tự giảm dần ..."); foreach (string str trong myLen) {Console.WriteLine (str); }}} 

Đầu ra

 Thứ tự tăng dần ... bustruckbicyclemotorbike Thứ tự tăng dần ... motobicycletruckbus