Share c# list sort

thuyphuongphoto

New member
#C ##List #sort #Programming #tutorial ## C #Danh sách sắp xếp

** C# Danh sách sắp xếp **

Trong C#, bạn có thể sắp xếp một danh sách bằng phương thức `sort ()`.Phương thức `sort ()` lấy một đại biểu làm tham số của nó.Đại biểu chỉ định logic để so sánh hai yếu tố trong danh sách.Phương thức `sort ()` sẽ sắp xếp danh sách theo thứ tự tăng dần theo mặc định.Tuy nhiên, bạn có thể chỉ định một thứ tự giảm dần bằng cách chuyển tham số `Reverse` sang phương thức` sort () `.

Mã sau đây cho thấy cách sắp xếp danh sách các chuỗi theo thứ tự tăng dần:

`` `C#
Danh sách <String> MYLIST = Danh sách mới <String> ();
mylist.add ("Apple");
mylist.add ("chuối");
mylist.add ("anh đào");

// Sắp xếp danh sách theo thứ tự tăng dần
mylist.sort ();

// In danh sách được sắp xếp
foreach (mục chuỗi trong mylist)
{
Console.WriteLine (Mục);
}
`` `

Đầu ra của mã trên sẽ là:

`` `
Quả táo
Chuối
quả anh đào
`` `

Bạn cũng có thể sắp xếp một danh sách theo một tài sản tùy chỉnh.Ví dụ: mã sau đây sắp xếp một danh sách các đối tượng theo thuộc tính `name`:

`` `C#
Danh sách <ByObject> MYLIST = DANH SÁCH MỚI <MYOBject> ();
mylist.add (MYOBject mới {name = "john"});
mylist.add (MYOBject mới {name = "Mary"});
mylist.add (MYOBject mới {name = "bob"});

// Sắp xếp danh sách theo thuộc tính tên
mylist.sort ((o1, o2) => o1.name.compareto (o2.name));

// In danh sách được sắp xếp
foreach (mục myObject trong mylist)
{
Console.WriteLine (item.name);
}
`` `

Đầu ra của mã trên sẽ là:

`` `
Bob
John
Mary
`` `

## hashtags

* #C#
* #Danh sách
* #Loại
* #Programming
* #tutorial
=======================================
#C# #List #sort #Programming #tutorial ##C# List Sort

**C# List Sort**

In C#, you can sort a list using the `Sort()` method. The `Sort()` method takes a delegate as its parameter. The delegate specifies the logic for comparing two elements in the list. The `Sort()` method will sort the list in ascending order by default. However, you can specify a descending order by passing the `Reverse` parameter to the `Sort()` method.

The following code shows how to sort a list of strings in ascending order:

```c#
List<string> myList = new List<string>();
myList.Add("Apple");
myList.Add("Banana");
myList.Add("Cherry");

// Sort the list in ascending order
myList.Sort();

// Print the sorted list
foreach (string item in myList)
{
Console.WriteLine(item);
}
```

The output of the above code will be:

```
Apple
Banana
Cherry
```

You can also sort a list by a custom property. For example, the following code sorts a list of objects by the `Name` property:

```c#
List<MyObject> myList = new List<MyObject>();
myList.Add(new MyObject { Name = "John" });
myList.Add(new MyObject { Name = "Mary" });
myList.Add(new MyObject { Name = "Bob" });

// Sort the list by the Name property
myList.Sort((o1, o2) => o1.Name.CompareTo(o2.Name));

// Print the sorted list
foreach (MyObject item in myList)
{
Console.WriteLine(item.Name);
}
```

The output of the above code will be:

```
Bob
John
Mary
```

## Hashtags

* #C#
* #List
* #sort
* #Programming
* #tutorial
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top