2014-11-24 39 views
-4

我想加入c#文本文件中的名字和姓氏,但只是在一个内容中有15个名字和15个姓氏,像这样,并且必须按照alphabelt无论是名字或姓氏加入阵列中的名字和姓氏

emily 
adrian 
camille 
lim 
ong 
ang 

如果第一theт输出必须是如果它是第一个名字

adrian ong 
camille ang 
emily lim 

如果最后

Ang camille 
lim emily 
ong adrian 
+0

我想你可以使用Enumerable.Range但让我们先来看看你的代码。并请RTFM! http://stackoverflow.com/help/how-to-ask – 2014-11-24 03:47:19

+0

你能从文本文件共享几条记录吗?这个问题很混乱。 – 2014-11-24 03:47:20

+1

加油。 'File.ReadAllLines','string []'数组和'for'循环就是你需要完成的。不要求代码,尝试写下代码并在遇到问题时回来。但**请先尝试解决问题!**。 – MarcinJuraszek 2014-11-24 03:51:53

回答

1

以下内容提供了所需的输出,并允许您通过布尔标志控制姓氏或姓氏,您可以将其绑定到参数或输入中。这里是.NET小提琴:https://dotnetfiddle.net/bHuOWQ

using System; 
using System.Linq; // Utilizing linq to perform sorting 
using System.Collections.Generic; // Utilizing generic List to accumulate objects 

// Statically provide sample data, but should use File.ReadAllLines when loading from file 
string[] records = new string[] { //File.ReadAllLines to get from the file 
    "emily", 
    "adrian", 
    "camille", 
    "lim", 
    "ong", 
    "ang" 
}; 

bool sortByFirstName = true; // Set to false if by last name 
int range = records.Length/2; // Since source data splits first and last names into same list, use value to define split between where first name stops and last name starts 
var items = new List<string>(); // Define list to contain the sortable items 

// Iterate through the first and last names to gather the sortable names 
for (int i = 0; i < range; i++) 
{ 
    if (sortByFirstName == true) // If sorting by first name, format entry as "first last" 
     items.Add(string.Format("{0} {1}", records[i], records[i+range])); 
    else // Otherwise, sort by last name, format entry as "last first" 
     items.Add(string.Format("{1} {0}", records[i], records[i+range])); 
} 
var sortedItems = items.OrderBy(s => s); // Use Linq to perform sorting 
foreach (var s in sortedItems) 
    Console.WriteLine(s); // Output the results 

产量:

adrian ong 
camille ang 
emily lim 
+0

Downvote for help没有技术问题的答案 - 真的吗?哲学差异... – 2014-11-24 13:53:43

+0

根据评论要求添加评论 – 2014-11-24 14:01:50

+0

这样比较好。是的,投票确实是一个哲学问题 - 没有官方的指导方针。 – 2014-11-24 15:27:05