Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort String array in custom order

Tags:

arrays

c#

sorting

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

like image 212
Prashanth KM Avatar asked Dec 28 '22 00:12

Prashanth KM


2 Answers

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;
        }
    }
}
like image 83
Adam Houldsworth Avatar answered Jan 12 '23 06:01

Adam Houldsworth


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";
like image 35
Nikhil Agrawal Avatar answered Jan 12 '23 06:01

Nikhil Agrawal