>  기사  >  백엔드 개발  >  C#에서 목록을 정렬하는 방법은 무엇입니까?

C#에서 목록을 정렬하는 방법은 무엇입니까?

PHPz
PHPz앞으로
2023-09-12 16:13:021217검색

C#에서 목록을 정렬하는 방법은 무엇입니까?

C#에서 목록을 정렬하려면 Sort() 메서드를 사용하세요.

먼저 목록을 만들어 보겠습니다. -

List<string> myList = new List<string>();

이제 요소를 추가합니다. -

myList.Add("Audi");
myList.Add("BMW");
myList.Add("Chevrolet");
myList.Add("Hyundai");

Sort() 메서드를 사용하여 목록 정렬 -

myList.Sort();

다음 예에서는 C#에서 목록을 정렬하는 방법을 보여줍니다. -

Example

라이브 데모

using System;
using System.Collections.Generic;

class Demo {
   static void Main() {
      List<string> myList = new List<string>();

      myList.Add("Audi");
      myList.Add("BMW");
      myList.Add("Chevrolet");
      myList.Add("Hyundai");

      myList.Sort();

      foreach (string value in myList) {
         Console.WriteLine(value);
      }
   }
}

출력

Audi
BMW
Chevrolet
Hyundai

위 내용은 C#에서 목록을 정렬하는 방법은 무엇입니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 tutorialspoint.com에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제