Sort String array in custom order

那年仲夏 提交于 2020-01-11 09:38:14

问题


I want to sort a fixed set of strings like "Text files", "Image files", "Audio files", "Video files", "Application Files", "Other files" in a string array in the same order I have mentioned.

Example1, if my string array input is like this

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

my output array should have values like this

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

Example 2, if my string array input is like this

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

my output array should have values like this

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

Please can somebody help me in achieving this


回答1:


Since not very much is clear from what you want. So i have taken into consideration that there will be no repetitions in inputval.

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

Let us say

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

Do this

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

So outputval will be

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



回答2:


This crude implementation using IComparer<string> supplied to Array.Sort works. There are various potential shortcomings, but I'll leave these to you (like strings needing to match exactly, otherwise they won't sort correctly).

It simply uses an internal list of strings that represent the correct order, and then compares their ordinals in that list with each other.

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;
        }
    }
}



回答3:


Implement an ICompare and then you can use OrderBy with an ICompare to get your custom sort. Check MSDN ICompare article

I.e. something like,

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
    }
}



回答4:


just have the strings with numbers appended in the beginning and add it to a sorted list..

like "0,Text files", "1,Image files", "2,Audio files", "3,Video files", "4,Application Files", "4,Other files"

and then while using remove the string before the ","..



来源:https://stackoverflow.com/questions/10781914/sort-string-array-in-custom-order

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!