Created
April 30, 2020 05:27
-
-
Save DongguemYoo/6615c406d1a9048195c004f36cef0b1f to your computer and use it in GitHub Desktop.
코딩테스트 연습 문자열 내 마음대로 정렬하기
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
와 개무식하게 풀엇네 | |
문자열 정렬하는 거 공부 필요!!! | |
string[] answer = new string[] { }; | |
Dictionary<string, char> dic = new Dictionary<string, char>(); //using System.Collections.Generic; | |
for (int i = 0; i < strings.Length; i++) | |
{ | |
dic.Add(strings[i], strings[i][n]); | |
} | |
//System.Linq OrderBy용 | |
//딕셔너리 정렬 key순서대로 or value 순서대로 정렬가능 | |
var tmp = dic.OrderBy(x => x.Key); string 기준으로 정렬한다 | |
var tmp2 = tmp.OrderBy(x => x.Value); string 기준으로 정렬된 배열을 n번째char 기준으로 정렬한다 | |
//dic.OrderBy(x => x.Key); | |
List<string> tmp3 = new List<string>(); | |
foreach (var x in tmp2) | |
{ | |
tmp3.Add(x.Key); | |
} | |
answer = tmp3.ToArray(); | |
return answer; | |
######와 미친 다른사람이 한거!! | |
string[] answer = strings.OrderBy(x => x).OrderBy(x => x.ElementAt(n)).ToArray(); | |
설명 | |
strings.OrderBy(x => x) | |
먼저 string을 기준으로 정렬을 한다 | |
정렬된 기준에서 n번째해당하는 문자열로 정렬한다 | |
OrderBy(x => x.ElementAt(n)) | |
완벽~ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment