C#:自定义数组排序(C#: custom array sorting)

编程入门 行业动态 更新时间:2024-10-23 14:34:01
C#:自定义数组排序(C#: custom array sorting)

我想对一个目录中的字符串数组进行排序,给定一个自定义映射(它实际上是基于它们的扇区对股票名称进行排序)。 我不确定用什么数据结构来表示映射,以及如何编写自定义排序方法。

所以举个例子,假设我有下面的字符串数组:

string[] fileNames = "bac.csv", "c.csv", "cvx.csv", "java.csv", "msft.csv", "xom.csv";

这里是映射:

{"bac", "c"} => 0 {"msft", "java"} => 1 {"xom", "cvx"} => 2

我想要string [] customSort(string [] fileNames)返回以下内容:

"bac.csv", "c.csv", "java.csv", "msft.csv", "xom.csv", "cvx.csv"

您将使用什么数据结构来表示映射,以及编写排序方法的优雅方式是什么?

I would like to sort an array of strings in a directory, given a custom mapping (it's actually a sorting of stock names based on their sector). I am unsure of what data structures to use to represent the mapping, and how to write the custom sort method.

So for instance, suppose I had the following string array:

string[] fileNames = "bac.csv", "c.csv", "cvx.csv", "java.csv", "msft.csv", "xom.csv";

And here are the mappings:

{"bac", "c"} => 0 {"msft", "java"} => 1 {"xom", "cvx"} => 2

I would like string[] customSort(string[] fileNames) to return the following:

"bac.csv", "c.csv", "java.csv", "msft.csv", "xom.csv", "cvx.csv"

What data structure would you use to represent the mappings, and what's an elegant way of writing the sort method?

最满意答案

Array.Sort允许你指定一个键的数组,所以你可以做类似...

int[] keys = new int[fileNames.Length]; Dictionary<string, int> mapping = new Dictionary<string, int>(StringComparer.CurrentCultureIngoreCase); // set up our mappings like so mapping.Add("bac", 0); mapping.Add("c", 0); mapping.Add("msft", 1); mapping.Add("java", 1); mapping.Add("xom", 2); mapping.Add("cvx", 2); // etc for(int i=0; i < keys.Length; i++) { string token = System.IO.Path. GetFileNameWithoutExtension(fileNames[i]); int mappingKey; if(!mapping.TryGetValue(token, out mappingKey)) mappingKey = int.MaxValue; keys[i] = mappingKey; } Array.Sort<int, string>(keys, fileNames);

只需修改keys[i] = -1; 语句从给定token变量的映射中获取适当的值。

Array.Sort allows you to specify an array of keys, so you can do something like...

int[] keys = new int[fileNames.Length]; Dictionary<string, int> mapping = new Dictionary<string, int>(StringComparer.CurrentCultureIngoreCase); // set up our mappings like so mapping.Add("bac", 0); mapping.Add("c", 0); mapping.Add("msft", 1); mapping.Add("java", 1); mapping.Add("xom", 2); mapping.Add("cvx", 2); // etc for(int i=0; i < keys.Length; i++) { string token = System.IO.Path. GetFileNameWithoutExtension(fileNames[i]); int mappingKey; if(!mapping.TryGetValue(token, out mappingKey)) mappingKey = int.MaxValue; keys[i] = mappingKey; } Array.Sort<int, string>(keys, fileNames);

Just modify the keys[i] = -1; statement to get the proper value from your mappings given the token variable.

更多推荐

本文发布于:2023-04-29 02:14:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1334557.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   数组   custom   array   sorting

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!