对绑定到 ListBox 的按字母顺序排列的 ObservableCollection 进行排序

Sort Alphabetic ObservableCollection binded to ListBox

我使用 API 编写了一个使用病毒总数扫描文件的小功能。它运行良好,但扫描结果未按字母顺序排序。

这是我的代码:

public void init(FileReport _scanResult) {

        try {

            if (_scanResult.ResponseCode == ReportResponseCode.Present) {
                foreach (ScanEngine scan in _scanResult.Scans) {
                    if (scan.Detected == true) {
                        howMany++;
                        _scanResultItems.Add(new ScanResultItems {
                            AvName = scan.Name,
                            AvResult = new Uri("/Images/inWatch.avNOT.png", UriKind.RelativeOrAbsolute),
                            AvStatus = "BEDROHUNG!"
                        });
                        Width = 390;
                    }
                    else {
                        _scanResultItems.Add(new ScanResultItems {
                            AvName = scan.Name,
                            AvResult = new Uri("/Images/inWatch.avOK.png", UriKind.RelativeOrAbsolute),
                            AvStatus = "OK"
                        });
                    }
                }

                lstScanResults.ItemsSource = _scanResultItems.OrderBy(item => item.AvName).ToList();
            }
        }
        catch(Exception ex) {
            GeneralSettings.LogException(ex);
        }

感谢您的回答!

您可以使用 System.Linq 对结果集中的项目进行排序; OrderBy() 方法,像这样:

_scanResultItems = _scanResultItems.OrderBy(item => item.Name).ToList();