如何按长度字母顺序对字符串数组进行排序?

发布于 2024-07-26 04:55:26 字数 275 浏览 5 评论 0原文

按字母顺序按长度排列的意思如下:

给出: {“=”、“==>>”、“=>”、“=>”、“!>” 我

想退出:

!>
=
=>
=>>
==>>

我目前只使用 OrderBy(x => x.Length).ToArray()

有人有更好的 lambda 吗?

这合理可能吗? 哈哈,我不确定这里的规则约定是什么:-(

By alphabetically by length I mean as follows:

given:
{ "=", "==>>", "=>>", "=>", "!>" }

I want to get out:

!>
=
=>
=>>
==>>

I'm currently just using OrderBy(x => x.Length).ToArray()

anyone got a better lambda?

Is this reasonably possible? lol, I'm not sure what the rule conventions here would be :-(

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

一枫情书 2024-08-02 04:55:26

您可以编写自己的字符串比较器来执行此操作,这满足您发布的两组要求。

    public class MyStringComparer : IComparer<string>
    {
        #region IComparer<string> Members

        public int Compare(string x, string y)
        {
            if (x[0] == y[0])
            {
                return x.Length.CompareTo(y.Length);
            }
            else return x[0].CompareTo(y[0]);
        }

        #endregion
    }

a bcde bcd b bced cb

将产生:

ab bcd bcde bced cb

and { "=", "==>>", "=>>", "=>", "!>" }

产生:

!>
=
=>
=>>
==>>

通过调用: myStringCollection.OrderBy(x=>x, new MyStringComparer).ToArray();

Writing your own string comparer you can do this, which meets both set of requirements you've posted.

    public class MyStringComparer : IComparer<string>
    {
        #region IComparer<string> Members

        public int Compare(string x, string y)
        {
            if (x[0] == y[0])
            {
                return x.Length.CompareTo(y.Length);
            }
            else return x[0].CompareTo(y[0]);
        }

        #endregion
    }

a bcde bcd b bced cb

would yield:

a b bcd bcde bced cb

and { "=", "==>>", "=>>", "=>", "!>" }

yields:

!>
=
=>
=>>
==>>

by calling: myStringCollection.OrderBy(x=>x, new MyStringComparer).ToArray();

初懵 2024-08-02 04:55:26

如果您想先按 Alpha 排序,然后按长度排序,请使用 OrderBy 和 ThenBy 方法:

var strings = new List<string> { "=", "==>>", "=>>", "=>", "!>" };
strings = strings.OrderBy( x => x ).ThenBy( x => x.Length ).ToList();
strings.ForEach( Console.WriteLine );

If you want to sort by Alpha then by length, use the OrderBy and ThenBy methods:

var strings = new List<string> { "=", "==>>", "=>>", "=>", "!>" };
strings = strings.OrderBy( x => x ).ThenBy( x => x.Length ).ToList();
strings.ForEach( Console.WriteLine );
几味少女 2024-08-02 04:55:26

我正在使用一个列表,并且一直在尝试在线搜索解决方案。 我发现没有任何东西对我有用。 我决定创建一种按长度对元素进行排序的方法。 为了首先按字母顺序对其进行排序,我使用集合排序。

    subfolders.Sort();
    public void SortByLength(List<string> subfolders)
    {
        int l = 0;
        int l2 = 0;
        for (int i = subfolders.Count() - 1; i != 0; i--)
        {
            l = subfolders[i].Length;
            if (i != subfolders.Count() - 1 && l2 < l)
            {
                string folder = subfolders[i + 1];
                subfolders.RemoveAt(i + 1);
                subfolders.Insert(0, folder);
            }
            l2 = l;
        }
    }

I am using a list and I have been trying to search online for a solution for this. Nothing that i found worked for me. I decided to create a method that sorts the elements by length. To sort it first by alphabetical order I use a collection sort.

    subfolders.Sort();
    public void SortByLength(List<string> subfolders)
    {
        int l = 0;
        int l2 = 0;
        for (int i = subfolders.Count() - 1; i != 0; i--)
        {
            l = subfolders[i].Length;
            if (i != subfolders.Count() - 1 && l2 < l)
            {
                string folder = subfolders[i + 1];
                subfolders.RemoveAt(i + 1);
                subfolders.Insert(0, folder);
            }
            l2 = l;
        }
    }
强辩 2024-08-02 04:55:26

您可以使用以下方法对数组进行排序。 这里我用中间字母对数组进行排序。
我认为这会有所帮助。

string[] names = { "Rajeshwar","Raj", "Vijay", "Sunil","Dhiresh","Sonmani" };
names = names.OrderBy(x => x.Substring(x.Length/2,1)).ToArray();

You can use following to sort the array. Here I am sorting the array with middle letter.
I think this will help.

string[] names = { "Rajeshwar","Raj", "Vijay", "Sunil","Dhiresh","Sonmani" };
names = names.OrderBy(x => x.Substring(x.Length/2,1)).ToArray();
浅忆 2024-08-02 04:55:26

我使用您提供的测试数据尝试了 OrderBy(x => x) ,这完全产生了您想要的输出。

如果它是您从一开始就有的数组,您只需使用 Array.Sort 方法:

Array.Sort(data);

编辑:
上述内容不适用于您在问题中输入的新数据。 等待有关数据的澄清(如对问题的评论)。

I tried OrderBy(x => x) with the test data that you provided, and that produces exactly the output that you want.

If it's an array that you have from start, you can just use the Array.Sort method:

Array.Sort(data);

Edit:
The above does not apply to the new data that you have put in the question. Awaiting a clarification about the data (as commented to the question).

蓝颜夕 2024-08-02 04:55:26

我不太清楚你的问题,但你似乎想首先按字符串长度排序,然后按字母位置排序。

您可以尝试将其作为 LINQ 解决方案:

vals = vals.OrderBy(str => str.Length).ThenBy(str => str).ToArray();

如果您想要一个不使用 LINQ 的(相当高效的)解决方案(尽管仍然可以很好地使用 lambda 表达式),您可以尝试以下操作:

Array.Sort(vals, (strA, strB) =>
{
    var lengthComp = Comparer<int>.Default.Compare(strA.Length, strB.Length);
    if (lengthComp == 0)
        return string.Compare(strA, strB);
    return lengthComp;
});

希望有所帮助。

I'm not totally clear about your question, but it seems you want to sort firstly by string length, then by alphabetical position.

You could try this as a LINQ solution:

vals = vals.OrderBy(str => str.Length).ThenBy(str => str).ToArray();

If you want a (rather more efficient) solution that does not use LINQ (though still makes nice use of lambda expressions), you could try the following:

Array.Sort(vals, (strA, strB) =>
{
    var lengthComp = Comparer<int>.Default.Compare(strA.Length, strB.Length);
    if (lengthComp == 0)
        return string.Compare(strA, strB);
    return lengthComp;
});

Hope that helps.

撕心裂肺的伤痛 2024-08-02 04:55:26

使用标准字符串比较函数进行排序应该可以做到这一点。

C 中的 strncmp(

) C++ 中的 String.compare(

) Java 中的 String.compareTo()

都可以用于此目的。

Using a standard string compare function for sorting should do exactly that.

strncmp() in C

String.compare() in C++

String.compareTo() in Java

all work for that purpose.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文