1

This is my class:

class Example {
    string GridValue;
}

In my code, i use a List that I want to sort. Let's say that this List has the following values:

{
    "P", 
    "Q", 
    "X", 
    "H", 
    "J", 
    "L"
}

And this is the output I expect after calling Sort():

{
    "X", 
    "Q", 
    "L", 
    "H", 
    "P", 
    "J"
}

How can I achieve this using the IComparer interface?

Thanks!

7
  • 2
    Well what's the order??? Is there a rule? Commented Dec 4, 2013 at 9:59
  • at least the rule is not clear, you should talk about that before we have to find the rule ourselves Commented Dec 4, 2013 at 10:00
  • Are those all the letters that are possible? Commented Dec 4, 2013 at 10:00
  • IComparer requires only that you can decide how two values compare to each other. Is your desired order arbitrary or can you create a function that can decide whether P is greater or smaller than J? Is the list of items fixed? Commented Dec 4, 2013 at 10:00
  • basically, the output (after sort()) i am giving you is the rule. So it means that, "X" has the highest value, then "Q", then "L" and so on. Commented Dec 4, 2013 at 10:02

2 Answers 2

5

Simple, you write a Comparer that takes your specific order into account:

public class ExampleComparer : IComparer<Example>
{
    private const string Order = "XQLHPJ";

    public int Compare(Example a, Example b)
    {
         int indexA = Order.IndexOf(a.GridValue);
         int indexB = Order.IndexOf(b.GridValue);
         if (indexA < indexB) { return -1; }
         else if (indexB < indexA) { return 1; }
         else { return 0; }
    }
} 
Sign up to request clarification or add additional context in comments.

6 Comments

what if I wanted that, if there is a few numbers in it to sort those by numerical order (giving priority to letters)?
@fobia: Then simply add "0123456789" to the Order constant.
well, but that's not the best way to do it I guess if I have values that could be random from 1 to 5000 right?
Aha, you mean not ordering by digits, but by entire numbers? Then you'll have to parse the input in your Compare method, determine if it is numeric or alphanumeric, etc. The point is that you can have any kind of comparison logic in the Compare method as long as you have properly defined rules.
@McFixit Done! Thanks.
|
0

If for every letter there is a known index SortOrder (and for unknown letter we can put default one):

class Example
{
    public string GridValue { get; set; }

    public int SortOrder { get; set; }
}

Then it is simple to use:

var list = new List<Example>();
var result = list.OrderBy(itm => itm.SortOrder);

And do not forget about language problems, if it is applicable, like Turkey Test.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.