4

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

6
  • 2
    On what basis do you want to display that arrays? Commented May 28, 2012 at 8:59
  • Do you mean that the Image files, Text files... are types and not strings? Commented May 28, 2012 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? Commented May 28, 2012 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 Commented May 28, 2012 at 9:11
  • I think you should answer to the first comment of @Nikhil Agrawal. What if on your example there is an extra string 'Video files'? Commented May 28, 2012 at 9:13

4 Answers 4

8

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;
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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

6 Comments

use StringComparer IgnoreCase with Contains method
@user671218 you have Application files with simple f in files but fixed_array with Application Files with capital F
How to do the same with objects? Any ideas?
@Azimuth: You need to override Equals and GetHashCode method of your class.
@NikhilAgrawal I implemented that by creating comparer class :)
|
1

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

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.