By alphabetically by length I mean as follows:
按字母顺序按长度表示如下:
given: { "=", "==>>", "=>>", "=>", "!>" }
给出:{“=”,“== >>”,“= >>”,“=>”,“!>”}
I want to get out:
我想出去:
!>
=
=>
=>>
==>>
I'm currently just using OrderBy(x => x.Length).ToArray()
我目前只使用OrderBy(x => x.Length).ToArray()
anyone got a better lambda?
谁有更好的lambda?
Is this reasonably possible? lol, I'm not sure what the rule conventions here would be :-(
这合理吗?大声笑,我不知道这里的规则约定是什么:-(
8 个解决方案
#1
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
一个bcde bcd b bced cb
would yield:
a b bcd bcde bced cb
一个bbcd bcde bced cb
and { "=", "==>>", "=>>", "=>", "!>" }
和{“=”,“== >>”,“= >>”,“=>”,“!>”}
yields:
!>
=
=>
=>>
==>>
by calling: myStringCollection.OrderBy(x=>x, new MyStringComparer).ToArray();
通过调用:myStringCollection.OrderBy(x => x,new MyStringComparer).ToArray();
#2
I don't think the sorting you're trying to do is any different than a standard dictionary sort, unless I'm missing something.
我不认为你试图做的排序与标准字典排序有什么不同,除非我遗漏了什么。
#3
If you want to sort by Alpha then by length, use the OrderBy and ThenBy methods:
如果要按Alpha排序然后按长度排序,请使用OrderBy和ThenBy方法:
var strings = new List<string> { "=", "==>>", "=>>", "=>", "!>" };
strings = strings.OrderBy( x => x ).ThenBy( x => x.Length ).ToList();
strings.ForEach( Console.WriteLine );
#4
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;
}
}
#5
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();
#6
I tried OrderBy(x => x)
with the test data that you provided, and that produces exactly the output that you want.
我使用您提供的测试数据尝试了OrderBy(x => x),并且产生了您想要的输出。
If it's an array that you have from start, you can just use the Array.Sort
method:
如果它是从头开始的数组,您可以使用Array.Sort方法:
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).
编辑:以上内容不适用于您在问题中添加的新数据。等待对数据的澄清(如对问题的评论)。
#7
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:
您可以尝试将其作为LINQ解决方案:
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:
如果你想要一个(更有效的)不使用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;
});
Hope that helps.
希望有所帮助。
#8
Using a standard string compare function for sorting should do exactly that.
使用标准字符串比较函数进行排序应该完全相同。
strncmp() in C
str中的strncmp()
String.compare() in C++
C ++中的String.compare()
String.compareTo() in Java
Java中的String.compareTo()
all work for that purpose.
一切都是为了这个目的。
#1
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
一个bcde bcd b bced cb
would yield:
a b bcd bcde bced cb
一个bbcd bcde bced cb
and { "=", "==>>", "=>>", "=>", "!>" }
和{“=”,“== >>”,“= >>”,“=>”,“!>”}
yields:
!>
=
=>
=>>
==>>
by calling: myStringCollection.OrderBy(x=>x, new MyStringComparer).ToArray();
通过调用:myStringCollection.OrderBy(x => x,new MyStringComparer).ToArray();
#2
I don't think the sorting you're trying to do is any different than a standard dictionary sort, unless I'm missing something.
我不认为你试图做的排序与标准字典排序有什么不同,除非我遗漏了什么。
#3
If you want to sort by Alpha then by length, use the OrderBy and ThenBy methods:
如果要按Alpha排序然后按长度排序,请使用OrderBy和ThenBy方法:
var strings = new List<string> { "=", "==>>", "=>>", "=>", "!>" };
strings = strings.OrderBy( x => x ).ThenBy( x => x.Length ).ToList();
strings.ForEach( Console.WriteLine );
#4
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;
}
}
#5
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();
#6
I tried OrderBy(x => x)
with the test data that you provided, and that produces exactly the output that you want.
我使用您提供的测试数据尝试了OrderBy(x => x),并且产生了您想要的输出。
If it's an array that you have from start, you can just use the Array.Sort
method:
如果它是从头开始的数组,您可以使用Array.Sort方法:
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).
编辑:以上内容不适用于您在问题中添加的新数据。等待对数据的澄清(如对问题的评论)。
#7
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:
您可以尝试将其作为LINQ解决方案:
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:
如果你想要一个(更有效的)不使用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;
});
Hope that helps.
希望有所帮助。
#8
Using a standard string compare function for sorting should do exactly that.
使用标准字符串比较函数进行排序应该完全相同。
strncmp() in C
str中的strncmp()
String.compare() in C++
C ++中的String.compare()
String.compareTo() in Java
Java中的String.compareTo()
all work for that purpose.
一切都是为了这个目的。