我有一个通用的
List<MyClass>
Run Code Online (Sandbox Code Playgroud)
where MyClass属性InvoiceNumber包含如下值:
200906/1
200906/2
..
200906/10
200906/11
200906/12
我的名单必然是一个
BindingList<T>
Run Code Online (Sandbox Code Playgroud)
支持使用linq进行排序:
protected override void ApplySortCore(
PropertyDescriptor property, ListSortDirection direction)
{
_sortProperty = property;
_sortDirection = direction;
var items = this.Items;
switch (direction)
{
case ListSortDirection.Ascending:
items = items.OrderByDescending(x => property.GetValue(x)).ToList();
break;
case ListSortDirection.Descending:
items = items.OrderByDescending(x => property.GetValue(x)).ToList();
break;
}
this.Items = items;
}
Run Code Online (Sandbox Code Playgroud)
但是默认的比较器排序(假设)如下:
200906/1
200906/10
200906/11
200906/12
200906/2
在这种情况下这是令人讨厌的.
现在我想用自己的方式IComparer<T>.它看起来像这样:
public class MyComparer : IComparer<Object>
{
public int Compare(Object stringA, Object …Run Code Online (Sandbox Code Playgroud) 我的表中有以下值:
ABC
ABC1
ABC2
ABC3 and so on...
ABC11
ABC12
ABC13 and so on..
ABC20
ABC21
ABC22 and so on..
Run Code Online (Sandbox Code Playgroud)
所以基本上我所拥有的是任何字符串值(不总是ABC,任何字符串值),可以跟随数字,也可以只是没有数字的字符串.
当我通过我的列asc从表顺序中选择*时,我得到以下结果:
ABC
ABC1
ABC11
ABC12
ABC13
ABC2
ABC20
ABC21
ABC22
ABC3
ABC31
ABC32
Run Code Online (Sandbox Code Playgroud)
我需要用数字排序:
ABC
ABC1
ABC2
ABC3
ABC11
ABC12
ABC13
ABC20
ABC21
ABC22
ABC31
ABC32
Run Code Online (Sandbox Code Playgroud)
如何实现这一目标?
例如,我想分开:
OS234去OS和234AA4230去AA和4230我使用了以下简单的解决方案,但我确信应该有一个更有效和更强大的解决方案.
private void demo()
{ string cell="ABCD4321";
int a = getIndexofNumber(cell);
string Numberpart = cell.Substring(a, cell.Length - a);
row = Convert.ToInt32(rowpart);
string Stringpart = cell.Substring(0, a);
}
private int getIndexofNumber(string cell)
{
int a = -1, indexofNum = 10000;
a = cell.IndexOf("0"); if (a > -1) { if (indexofNum > a) { indexofNum = a; } }
a = cell.IndexOf("1"); if (a > -1) { if (indexofNum > a) …Run Code Online (Sandbox Code Playgroud) 从昨天开始,我一直在为我的问题寻找解决方案.我正在构建MVVM模式的问题.我有两个usercontrol,它们都包含一个列表框.
第一个用户控件称为SearchView,它包含项目名称列表框,用户可以选择并保存到应用程序本地数据库.

添加所选项目时会触发一个事件,该事件通知名为"ProjectView"的第二个usercontrol.此用户控件只显示本地保存的项目.见下图.

问题是我希望能够在项目视图中按名称升序列表框.因此,如果用户首先添加"测试项目2"并且后面添加"测试项目1",则"测试项目1"将显示在列表框的顶部.
我曾尝试使用ICollectionView和ListCollectionView,但我现在非常非常困惑.
所以现在我的代码看起来像这样,在需要对列表框进行排序的ProjectViewModel中:
public ProjectViewModel() {
this.collectionView = CollectionViewSource.GetDefaultView(this.Projects);
}
private ObservableCollection<ProjectWrapper> _project = new ObservableCollection<ProjectWrapper>();
public ObservableCollection<ProjectWrapper> Projects
{
get { return _project; }
set
{
_project = value;
OnPropertyChanged("Projects");
}
}
Run Code Online (Sandbox Code Playgroud)
XAML代码:
<UserControl.Resources>
<CollectionViewSource x:Key="cvs" Source="{Binding Path=Projects}">
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="ProjectWrapper.Project.Name" />
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
</UserControl.Resources>
<ListBox Name="ProjectsList" ItemsSource="{Binding Source={StaticResource cvs}}" SelectedItem="{Binding Path=SelectedProject}" HorizontalContentAlignment="Stretch" BorderThickness="0" Grid.Row="1" Grid.RowSpan="3" Margin="0,0.4,-0.2,27.8">
<ListBox.ItemTemplate>
<DataTemplate>
<DockPanel>
<TextBlock Text="{Binding Path=ProjectModel.Name}" HorizontalAlignment="Left" VerticalAlignment="Center" Padding="3,2,0,0" />
<CheckBox IsChecked="{Binding Path=IsSelected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Right" VerticalAlignment="Center" Padding="0,2,5,0" Margin="0,2.5,0,0" …Run Code Online (Sandbox Code Playgroud) 我有一个简化的字符串,"12345"它被排序.字符串couild包含数字(0-9)或字母(az).在混合使用的情况下,自然排序顺序.我需要一种方法来验证这是否属实.
使用linq技术尝试:
string items1 = "2349"; //sorted
string items2 = "2476"; //not sorted, 6<>7
bool sorted1 = Enumerable.SequenceEqual(items1.OrderBy(x => x), items1); //true
bool sorted2 = Enumerable.SequenceEqual(items2.OrderBy(x => x), items2); //false
Run Code Online (Sandbox Code Playgroud)
但也可能有降序排序.
那么有更好的方法吗?
string items3 = "4321";
bool sorted3 = Enumerable.SequenceEqual(items3.OrderBy(x => x), items3) || Enumerable.SequenceEqual(items3.OrderByDescending(x => x), items3);
Run Code Online (Sandbox Code Playgroud)
检查字符串是否已排序?也许一些内置的解决方案?
我想使用索引对字符串进行排序,但是在使用第10个索引之后,在使用Sort()方法后,列表中的第一个索引之后添加了第10个/以后的索引时,它不起作用。
我已经尝试了下面的代码,但是没有用。
List<string> stringList = new List<string>();
foreach (ManagementObject disk in objectSearcher.Get() )
{
stringList.Add(string.Format("{0, -15} {1,-35} {2, -20}",
disk.GetPropertyValue("Index"),
disk.GetPropertyValue("Model"),
diskSize));
}
stringList.Sort();
Run Code Online (Sandbox Code Playgroud)
在上述情况下,该代码适用于0-9索引,但对于以后的索引,则无法正常工作。
我有一个包含字段编号的对象列表。该字段是一个包含浮点数的字符串:0.5、0.2、10.0、2.2、2.1、1.1、1.0、2.12、1.14。我希望这个列表根据这个数字排序(后代),所以我有这个输出:10.0、2.2、2.12、2.1、1.14、1.1、1.0、0.5、0.2。
我试图这样做:
List<MyObject> sortedList = myUnsortedList.OrderyDescending(x => x.Number).ToList();
另外,我尝试过像这样使用 PadLeft (因为该用户建议/sf/answers/447756641/):
int maxlen = myUnsortedList.Max(x => x.Number.Length);
List<MyObject> sortedList = myUnsortedList.OrderyDescending(x => x.Number.PadLeft(maxlen, '0'));
但在这两种情况下,我都得到了错误的结果,例如:2.12、10.0、2.2、2.1、1.14、1.1、1.0、0.5、0.2。
你知道如何解决这个问题吗?
我一直在研究对象的排序,需要对字符串进行排序,该字符串具有整数值,如"1","2".但LINQ OrderBy或SOrt inplace本身没有正确排序:
以下代码可以重现我的问题:
var listStr = new List<string>()
{
"1",
"100",
"12"
};
var sortedList = listStr.OrderBy(x => x);
var desList = listStr.OrderByDescending(x => x);
Run Code Online (Sandbox Code Playgroud)
这里,SortedList按以下顺序:"1","100","12"和deslist是"12","100","12"
我对C#排序中的字符串比较方式感到困惑.我对不工作的原因感兴趣.