如何根据第二个参数进行排序
本文关键字:排序 参数 第二个 何根 | 更新日期: 2023-09-27 18:21:19
我必须编写一个算法来根据第二个参数对名称进行排序,我无法获得逻辑如何操作,输入文件将包含这样的数据:
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 element
{
public string name;
public int numComp;
public string sex;
} ;
现在我正在考虑对numComp
进行排序,但如何处理algo,这是我无法理解的,有人能帮我吗,如果你知道任何其他最佳解决方案,请不要犹豫,谢谢
我所做的全部代码是:
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进行排序并根据需要打印输出
我假设您的意思是,作为第二个参数,结果应该按numCorp
排序,那么为什么不使用LINQ-Queries
:呢
var orderedList = storage.OrderBy(i => i.numCorp);
Take Any Sorting Algorithm(您可以在This Link找到一些解释的排序算法+代码),
实现它,并将比较功能替换为
int myElementCmpFunc(element a, element b){
return Int32.Compare(a.numCorp, b.numCorp);
}
并将此函数传递给排序算法。
注意在链接中,没有比较函数,但暗示要替换类似if (x <= pivot)
和if(myElementCmpFunc(x,pivot) <= 0)
的代码
注意2如果按numCorp进行比较是通用的比较函数,则可以用您的函数覆盖默认的比较函数并通过比较numCorp使a <= b
有效。
嘿,,你可以这样做
我已经创建了一个linqExtension方法,它从IEnumerable创建块(请参阅这篇文章,了解有关使用linq从IEnumarable输入将列表拆分为子列表创建块的更多详细信息)
检查这把小提琴的输出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);
}
}
}
我已经完成了,这就是的解决方案
class Program
{
struct element
{
public string name;
public string sex;
public int numComp;
public bool flag;
} ;
static void Main(string[] args)
{
int[] arr = { 5, 1, 6, 3, 4, 5, 8, 3, 9, 2, 6, 7 };
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; int[] storeNum = new int[words.Count() / 3];
element[] storage = new element[words.Count() / 3];
element[] storage2 = new element[words.Count() / 3];
foreach (string str in words)
{
if (counter == 0)
{
storage[pos].flag = false;
storage[pos].name = str;
counter++;
}
else if (counter == 1)
{
storeNum[pos] = storage[pos].numComp = Int32.Parse(str);
counter++;
}
else if (counter == 2)
{
storage[pos].sex = str;
counter = 0; pos++;
}
}
int[] mergeSorted = mergeSort(storeNum); //How to proceed here ?
int posit = 0, counterr = 0;
foreach (int num in mergeSorted)
{
for (int i = 0; i < 4; i++)
{
if (storage[i].numComp == num && storage[i].flag==false)
{
storage[i].flag = true;
storage2[posit++] = storage[i];
break;
}
counterr++;
}
}
Console.ReadKey();
}
private static int[] mergeSort(int[] arr)
{
if (arr.Count() <= 1)
{
return arr;
}
int mid = arr.Count() / 2;
int[] left = new int[mid];
int[] right = new int[arr.Count() - mid];
for (int i = 0; i < mid; i++)
{
left[i] = arr[i];
}
int k = 0;
for (int i = mid; i < arr.Count(); i++)
{
right[k++] = arr[i];
}
int[] leftVal = mergeSort(left);
int[] rightVal = mergeSort(right);
int[] merged = mergesort(leftVal, rightVal);
return merged;
}
private static int[] mergesort(int[] leftVal, int[] rightVal)
{
List<int> finalArr = new List<int>(leftVal.Count() + rightVal.Count());
List<int> left = new List<int>(leftVal);
List<int> right = new List<int>(rightVal);
while (left.Count != 0 && right.Count() != 0)
{
if (left[0] < right[0])
{
finalArr.Add(left[0]);
left.RemoveAt(0);
}
else
{
finalArr.Add(right[0]);
right.RemoveAt(0);
}
}
while (left.Count != 0)
{
finalArr.Add(left[0]);
left.RemoveAt(0);
}
while (right.Count != 0)
{
finalArr.Add(right[0]);
right.RemoveAt(0);
}
return finalArr.ToArray();
}
}
您可以按以下方式排序-
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;
}