나는 문자열 배열을 가지고 있습니다.
string[] Array = {a,b,c,d,e,a,b,c,d,e,a,b,c,d,e}
그리고 나는 목록이 있습니다.
List<string> l = {a,b,c,d,e}
다음과 같이 배열을 정렬해야합니다.
string[] sortedArray = {a,a,a,b,b,b,c,c,c,d,d,d,e,e,e}
내 작업은 :
public static List<string> SortByList(List<string> values, List<string> order)
{
return values.OrderBy(x => order.IndexOf(x)).ToList();
}
private void buttonGo_Click(object sender, EventArgs e)
{
List<string> alpha = new List<string>();
List<string> m = new List<string>();
string[] gamma = null;
using (StreamWriter sw = File.CreateText(pathSave))
{
foreach (string st in parts)
{
alpha.Add(st);
}
}
using (StreamReader sr = new StreamReader(pathSave, true))
{
gamma = File.ReadAllLines(pathSave);
foreach (string i in gamma)
{
l.Add(i);
}
m = SortByList(l, alpha);
}
using (StreamWriter sw = File.AppendText(pathSave))
{
sw.Write("---------------------------------------------------------------------");
foreach (string st in m)
{
sw.Write(st);
sw.Write("\r\n");
}
}
}
간단히 말해서은 alpha
문자열 요소 목록이며 lines
문자열 배열입니다. lines
.NET의 요소와 관련 하여 정렬하고 싶습니다 alpha
. 누군가 안내해 주시겠습니까? 감사
나는 당신이 다음과 같이하고 싶다고 생각합니다.
using System.IO;
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
var values = new List<string>{"a","b","c","d","e","a","b","c","d","e","a","b","c","d","e"};
var order = new List<string>{"a","b","c","d","e"};
var sortedList = SortByList(values, order);
foreach(var i in sortedList)
{
Console.WriteLine(i);
}
}
public static List<string> SortByList(List<string> values, List<string> order){
return values.OrderBy(x => order.IndexOf(x)).ToList();
}
}
출력 : aaabbbcccdddeee
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다