c# List Sort 질문드립니다.
조회수 3253회
List에 정보를 담아놨습니다. [0] [1] [2] [3] ......
이 리스트 안에 [0] - X,Y [1] - X,Y . . . . 이런 식으로 XY 좌표가 있는데 X 값으로 Sort를 하고 싶습니다. 알려주시면 감사하겠습낟 ㅜㅜ
-
(•́ ✖ •̀)
알 수 없는 사용자
1 답변
-
List<string>
이고, 콤마로 값을 구분해 놓으셨다고 가정하고 풀어 봤습니다.using System.Collections.Generic; public class Hello1 { public static void Main() { List<string> points = new List<string>(); points.Add("10.5 , 50"); points.Add("10.1, 10"); points.Add("9, 30.5"); /*sort에 비교하는 기능을 넣었습니다. list의 element를 읽어와서 ,로 구부한 다음 앞부분을 float로 변환해서 값을 비교합니다. 그걸 기준으로 정렬해요.*/ points.Sort((x, y) => float.Parse(x.Split(',')[0])>float.Parse(x.Split(',')[0])?1:-1); foreach(string str in points){ System.Console.WriteLine(str); } } }
댓글 입력