สั่งซื้อองค์ประกอบตามลำดับโดยใช้เมธอด ThenBy()
เรามีสตริงอาร์เรย์ดังต่อไปนี้
string[] str = { "AAA", "AAAA", "A", "AAAAA", "AAAAAAAAA" }; ตอนนี้ ใช้ Lambda Expressions และตั้งค่าเงื่อนไขภายในเมธอด ThenBy() เพื่อจัดเรียงสตริงตามจำนวนอักขระที่มี
IEnumerable<string> res = str.AsQueryable() .OrderBy(alp => alp.Length).ThenBy(alp => alp);
ตัวอย่าง
using System;
using System.Linq;
using System.Collections.Generic;
public class Demo {
public static void Main() {
string[] str = { "AAA", "AAAA", "A", "AAAAA", "AAAAAAAAA" };
IEnumerable<string> res = str.AsQueryable() .OrderBy(alp => alp.Length).ThenBy(alp => alp);
foreach (string arr in res)
Console.WriteLine(arr);
}
} ผลลัพธ์
A AAA AAAA AAAAA AAAAAAAAA