按照自定义顺序排序字符串数组

4

我想对一个固定字符串集合进行排序,例如“文本文件”,“图像文件”,“音频文件”,“视频文件”,“应用程序文件”,“其他文件”,使它们在字符串数组中按照我所提到的顺序排列。

例如1,如果我的字符串数组输入如下:

inputval[0] = "Other files";
inputval[1] = "Image files";
inputval[2] = "Text files";

我的输出数组应该像这样有值。
outputval[0] = "Text files";
outputval[1] = "Image files";
outputval[2] = "Other files";

例如,如果我的字符串数组输入如下:

inputval[0] = "Application files";
inputval[1] = "Image files";
inputval[2] = "Video files";

我的输出数组应该像这样具有值
outputval[0] = "Image files";
outputval[1] = "Video files";
outputval[2] = "Application files";

请有人帮我实现这个。

2
你想按照什么基础来显示那些数组? - Nikhil Agrawal
你的意思是 Image filesText files 等是类型而不是字符串吗? - chaliasos
图像文件,文本文件都是字符串值。基本上,我将获得用户输入为“其他文件;图像文件;文本文件;”或任何其他组合,我将其拆分为字符串数组(Split(';'))。但是一旦我拆分,我希望数组值按照我提到的顺序排列。如果您仍然不清楚,请告诉我。 - Prashanth KM
如果您将一个序列号与每个数组元素关联,并根据该编号对数组进行排序,那将是很好的。 - Milan Raval
我认为你应该回复@Nikhil Agrawal的第一条评论。如果在你的示例中有一个额外的字符串“视频文件”,会怎么样? - chaliasos
阅读此问题的人应注意,虽然发布的答案可行,并且如果预期输入很小,则并不太糟糕,但如果可能排序值的数量很大,则它们将非常低效。更好的方法是使用排序索引字典,在此答案中可以找到:https://stackoverflow.com/a/25546954 - Peter Duniho
4个回答

8

这个简陋的实现使用提供给Array.SortIComparer<string>可以工作。有各种潜在的缺点,但我会留给你自己解决(例如字符串需要完全匹配,否则它们将无法正确排序)。

它只是使用一个内部字符串列表来表示正确的顺序,然后将它们在该列表中的序数进行比较。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication61
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] files = new[] { "Audio Files", "Text Files", "Video Files", "Other Files", "Application Files" };
            Array.Sort(files, new FileComparer());
            Console.Read();
        }
    }

    class FileComparer : IComparer<string>
    {
        static List<string> OrderedFiles = new List<string> { "Text Files", "Image Files", "Audio Files", "Video Files", "Application Files", "Other Files" };

        public int Compare(string x, string y)
        {
            int xi = OrderedFiles.IndexOf(x);
            int yi = OrderedFiles.IndexOf(y);

            if (xi > yi)
                return 1;

            if (xi < yi)
                return -1;

            return 0;
        }
    }
}

1

实现一个 ICompare,然后您可以使用带有 ICompareOrderBy 来获取自定义排序。请检查 MSDN ICompare article

例如,像这样的东西:

public class MyCompare : ICompare<string>
{
    // Because the class implements IComparer, it must define a 
    // Compare method. The method returns a signed integer that indicates 
    // whether s1 > s2 (return is greater than 0), s1 < s2 (return is negative),
    // or s1 equals s2 (return value is 0). This Compare method compares strings. 
    public int Comapre(string s1, string s2)
    {
        // custom logic here
    }
}

1

由于您的需求不是很清晰,因此我考虑到 inputval 中不会有重复项。

string[] fixed_array =  { "Text files", "Image files", "Audio files", 
                        "Video files", "Application Files", "Other files" };

假设我们说

inputval[0] = "Other files";
inputval[1] = "Image files";
inputval[2] = "Text files";

请执行此操作

string[] outputval =
          fixed_array.Select(x => inputval.Contains(x) ? x : "-1")
                     .Where(x => x != "-1").ToArray();

所以outputval将会是

outputval[0] = "Text files";
outputval[1] = "Image files";
outputval[2] = "Other files";

Д╫©Г■╗StringComparerГ └IgnoreCaseД╦▌ContainsФ√╧ФЁ∙Ц─┌ - Damith
@user671218,你有一个名为Application files的文件夹,其中的文件名中字母"f"是小写的,但是fixed_array文件夹中的文件名是Application Files,其中字母"F"是大写的。 - Damith
如何使用对象实现相同的操作?有什么想法吗? - Azimuth
@Azimuth:你需要重写你的类的EqualsGetHashCode方法。 - Nikhil Agrawal
@NikhilAgrawal 我通过创建比较器类来实现了那个 :) - Azimuth
那是另一种方式,基本上我们需要的只是一种比较对象而不仅仅是参考检查的方法。 - Nikhil Agrawal

0

只需将数字附加在字符串开头并添加到已排序的列表中。

例如 "0,文本文件","1,图像文件","2,音频文件","3,视频文件","4,应用程序文件","4,其他文件"

然后在使用时删除逗号之前的字符串。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接