按大写字母排序 [英] Sorting according to Capital Latters

查看:89
本文介绍了按大写字母排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在尝试对包含文本的网格控制列进行排序.我有一个问题.当我按升序对所有内容进行排序时,将"AA"放在"aa"之前,将大写字母放在首位,然后将小写字母放在首位,如何将其反转?每个人都看到相同的问题还是有人解决了?它.

我希望升序排序结果为::

aa
bb
AA
Ab

记住不要在代码中使用IComparer.

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.

推荐答案

该问题未标记为对应于问题中提到的Grid control.
但是,我认为custom comparer可用于按要求对项目进行排序.
为了说明这个想法,我对上述项目进行了排列,并使用自定义比较器对其进行了排序,如下所示

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

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.


我将此解决方案作为单独的解决方案发布,因为它与我以前的解决方案不同,并且发布在较早的时间会很长,它们可能会混淆.

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.

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


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


这篇关于按大写字母排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆