[TIL] 114 [C#] list์ ๋ ฌ
์นดํ ๊ณ ๋ฆฌ: Til
list sort
list ์ ๋ ฌ (๋์ด์, ๊ฐ์ ์)
๊ตฌ์กฐ์ฒด(struct) OR class์ฌ์ฉ
class Program
{
public struct agename
{
public int age;
public string name;
public int index;
public agename(int age, string name, int index)
{
this.age = age;
this.name = name;
this.index = index;
}
}
static void Main()
{
string input = Console.ReadLine();
int testcase = Convert.ToInt32(input);
List<agename> list = new List<agename>();
int age = 0;
for (int i = 0; i < testcase; i++)
{
string agename = Console.ReadLine();
string[] agenames = agename.Split(' ');
age = Convert.ToInt32(agenames[0]);
list.Add(new agename(age, agenames[1],i));
}
list.Sort((a, b) =>
{
if (a.age != b.age)
return a.age.CompareTo(b.age); // ๋์ด์์ผ๋ก ์ ๋ ฌ
return a.index.CompareTo(b.index);
});
foreach (var agename in list)
{
Console.WriteLine($"{agename.age} {agename.name}");
}
Console.ReadLine();
}
}
- Sort() ๋ฉ์๋๋ ๋ ๊ฐ์ ๊ฐ์ฒด๋ฅผ ๋น๊ตํ๋ ๊ธฐ์ค์ ์ ๊ณต
- ๋น๊ต ํ ๊ทธ ๊ฒฐ๊ณผ์ ๋ฐ๋ผ ์ ๋ ฌํฉ๋๋ค.
์ก๋ด, ์ผ๊ธฐ?
์ด๋ ฅ์ ์ ๋ฆฌ, ์์์ ์์ฑ, ๋ฉด์ ์ค๋น์ค, ์ฝํ
๋๊ธ๋จ๊ธฐ๊ธฐ