我必须编写一个算法来根据第二个参数对名称进行排序,我无法得到逻辑怎么做,输入文件将包含如下数据:
Jervie, 12, M , Jaimy ,11, F, Tony , 23, M ,Janey , 11, F
输出必须是这样的:
Jaimy, 11, F , Janey, 11, F, Jervie ,12, M, Tony , 23, M
它们是根据第二个参数排序的,我已经读取了这个文件并将数据存储在struct array中:
struct element
{
public string name;
public int numComp;
public string sex;
} ;
现在我正在考虑进行排序numComp
,但是如何处理算法,这是我无法理解的,有人可以帮助我吗? ,如果你知道任何其他最佳解决方案,那么请不要犹豫,谢谢
我完成的完整代码是:
class Program
{
struct element
{
public string name;
public string sex;
public int numComp;
} ;
static void Main(string[] args)
{
string appRootDir = new DirectoryInfo(Environment.CurrentDirectory).Parent.Parent.FullName;
string text = System.IO.File.ReadAllText(appRootDir+"\\File.txt");
string[] words = text.Split(new Char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
int counter = 0 ,pos=0;
element[] storage = new element[words.Count()/3];
foreach(string str in words)
{
if(counter==0)
{
storage[pos].name = str;
counter++;
}
else if (counter == 1)
{
storage[pos].numComp = Int32.Parse(str);
counter++;
}
else if(counter==2)
{
storage[pos].sex = str;
counter = 0; pos++;
}
}
//Now how to sort on the basis of numComp and print the output as desired ?
Console.ReadKey();
}
}
现在如何根据numComp进行排序并根据需要打印输出?
答案 0 :(得分:4)
我假设您的意思是作为第二个参数,结果应该由numCorp
排序,那么为什么不使用LINQ-Queries
:
var orderedList = storage.OrderBy(i => i.numCorp);
答案 1 :(得分:4)
采用任何排序算法(你可以找到一些排序算法解释+代码This Link),
实现它,并将比较函数替换为
null
并将此函数传递给排序算法。
链接中 注意,没有比较功能,但暗示要替换
topologicalSort
与int myElementCmpFunc(element a, element b){
return Int32.Compare(a.numCorp, b.numCorp);
}
Note2 如果通过numCorp进行比较是一般比较函数,您可以使用您的函数覆盖默认比较函数,并通过比较numCorp来使if (x <= pivot)
有效。
答案 2 :(得分:1)
嘿,你可以这样做,
我创建了一个linq扩展方法,它从IEnumerable创建块(有关从IEnumerable输入Split List into Sublists with LINQ创建块的更多详细信息,请参阅此帖子)
检查此小提琴输出https://dotnetfiddle.net/VlkRot
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public class Program
{
struct element
{
public string name;
public string sex;
public int numComp;
} ;
public static void Main()
{
string input = "Jervie, 12, M , Jaimy ,11, F, Tony , 23, M ,Janey , 11, F";
var elements = input.Split(',').Where(x => !string.IsNullOrWhiteSpace(x))
.Select(x => x.Trim()).Chunk(3)
.Select(x =>
{
var i = x.ToList();
return new element
{
name = i[0],
numComp = int.Parse( i[1]),
sex = i[2],
};
});
var sorted = elements.OrderBy(x => x.numComp).ToList();
var temp = sorted.Select(x => x.name + ", " + x.numComp+", " + x.sex );
var output = string.Join(", ",temp);
Console.WriteLine(output);
}
}
public static class LinqExtension
{
public static IEnumerable<IEnumerable<T>> Chunk<T>(this IEnumerable<T> source, int chunksize)
{
while (source.Any())
{
yield return source.Take(chunksize);
source = source.Skip(chunksize);
}
}
}
答案 3 :(得分:0)
我已经完成了,这就是解决方案
<td class="numeric"><label class="switch">
@if($data->status=="active")
<input class="switch-input" type="checkbox" onClick="javascript:toggleStatus({{$data->id}})" checked="checked"/>
@else
<input class="switch-input" type="checkbox" onClick="javascript:toggleStatus({{$data->id}})"/>
@endif
<span class="switch-label" data-on="On" data-off="Off"></span>
<span class="switch-handle"></span>
</label>
<span id="status_txt{{$data->id}}" class="status_{{$data->status}}">@if($data->status=="inactive")<p style="color:red;">{{ ucfirst($data->status) }}</p>@else{{ ucfirst($data->status) }}@endif</p></span>
</td>
答案 4 :(得分:0)
您可以按以下方式排序 -
String [] input = {"Jervie,12,M" , "Jaimy,11,F", "Tony,23,M" ,"Janey,11,F"};
List<String> al= Arrays.asList(input);
Collections.sort(al, (o1, o2) -> Integer.parseInt(o1.split(",")[1]) - Integer.parseInt(o2.split(",")[1]));
或
private String [] sort (String [] input) {
PriorityQueue<String> pq = new PriorityQueue<>((o1, o2) -> Integer.parseInt(o1.split(",")[1]) - Integer.parseInt(o2.split(",")[1]));
for (String s : input) {
pq.add(s);
}
String [] res = new String[input.length];
int i=0;
while(!pq.isEmpty()){
res [i++] = pq.poll();
}
return res;
}