Click here to Skip to main content
15,893,508 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hello Everyone,

I am trying to sort my grid control column which contains text. I have one issue. When i am sorting everything in ascending order it puts "AA" before "aa", it is putting the Capital letters first and then the small letters, how to reverse this? does everyone see the same problem or has anyone solved. it.

I want the ascending sort result to be of type ::

aa
bb
AA
Ab

Remember Do not use IComparer in Code.
Posted
Updated 25-Apr-12 21:54pm
v3
Comments
Pete O'Hanlon 26-Apr-12 4:15am    
Why are you trying to avoid IComparer - it's the right tool to do the job. Would you use a melon to hammer in a nail? Of course you wouldn't, so why are you trying to avoid doing this.
Sergey Alexandrovich Kryukov 10-May-12 0:01am    
Absolutely. This is a totally incorrect request, an abuse.

Now, what would you say about this request (asking you as the most suspicious member I know :-):
http://www.codeproject.com/Questions/381777/Tech-Gig-Problem-of-Expert-Level

What do you think? Could it be an attempt to cheat or something like that?
--SA
Pete O'Hanlon 10-May-12 3:58am    
Oh it's definitely attempting to cheat.

The question is not tagged with the type of UI corresponding to the Grid control mentioned in the question.
However, I think a custom comparer can be used to sort the items as per requirement.
To illustrate the idea, I have taken an array with the above items and sorted it with a custom comparer as shown below

C#
void Main()
{
    string[] items = new [] {"AA","aa","Ab","bb"};
    Array.Sort(items, new ItemComparer());
}

public class ItemComparer : IComparer<string> {
    public int Compare(string x, string y){
        if (char.IsLower(x,0) && char.IsUpper(y,0) )
            return -1;
        else if (char.IsUpper(x,0) && char.IsLower(y,0))
            return 1;
        else
            return x.CompareTo(y);
    }
}
//Sorted array contents
//aa
//bb
//AA
//Ab

Hope, it may give some clue to implement similar functionality for the problem specified in the question.
 
Share this answer
 
This solution I posted as separate solution, as it is different from my earlier solution, and posting in the earlier make it very long and they may get mixed up.

C#
void Main()
{

    MyString[] items = new [] {new MyString("AA"), new MyString("aa"),
        new MyString("Ab"), new MyString("bb")};

    Array.Sort(items);
}
public struct MyString : IComparable<MyString> {
    public string Item;
    public MyString(string item){
        Item=item;
    }
    public override string ToString(){
        return Item;
    }
    public int CompareTo(MyString other){
        if (char.IsLower(Item,0) && char.IsUpper(other.Item,0) )
            return -1;
        else if (char.IsUpper(Item,0) && char.IsLower(other.Item,0))
            return 1;
        else
            return Item.CompareTo(other.Item);
    }
}
//Sorted array contents
//aa
//bb
//AA
//Ab
 
Share this answer
 
v2
C#
void Main()
{
    string[] items = new [] {"AA","aa","Ab","bb"};
    Array.Sort(items, new ItemComparer());
}

public class ItemComparer : IComparer<string> {
    public int Compare(string x, string y){
        if (char.IsLower(x,0) && char.IsUpper(y,0) )
            return -1;
        else if (char.IsUpper(x,0) && char.IsLower(y,0))
            return 1;
        else
            return x.CompareTo(y);
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900