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

share|improve this question
2  
On what basis do you want to display that arrays? – Nikhil Agrawal May 28 at 8:59
3  
What have you tried so far? – Slugart May 28 at 8:59
Do you mean that the Image files, Text files... are types and not strings? – Schaliasos May 28 at 9:01
Image files, Text files are string values.. basically I will get user input as "Other files;Image files;Text files;" or with any other combination which Im splitting into a string array (Split(';'). but once I split I want the array values to be in the same order as I have mentioned. Please let me know if u r still not clear? – Prashanth KM May 28 at 9:09
It will be good if you associate a serial order number with each array element and sort the array on that number – Milan Raval May 28 at 9:11
show 2 more comments
feedback

4 Answers

up vote 1 down vote accepted

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";
share|improve this answer
Nikhil.. this was not giving me all the values in outputval array.. – Prashanth KM May 28 at 9:25
use StringComparer IgnoreCase with Contains method – Damith May 28 at 9:28
@user671218 you have Application files with simple f in files but fixed_array with Application Files with capital F – Damith May 28 at 9:30
Thanks Damith, Nikhil.. This worked :) – Prashanth KM May 28 at 9:42
feedback

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;
        }
    }
}
share|improve this answer
This worked like charm.. Thnks Adam :) – Prashanth KM May 28 at 9:23
feedback

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
    }
}
share|improve this answer
feedback

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 ","..

share|improve this answer
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.