简体   繁体   English

如何根据其中一个按字母顺序对 4 个单独的字符串数组(具有相同的长度)进行排序? (在 C# 中)

[英]How can I sort 4 seperate string arrays (with the same lenght), according to one of them in alphabetical order? (in C#)

I have read out per WMI the installed programs on a remote PC.我已经根据 WMI 读出了远程 PC 上已安装的程序。 I have read the Program Name, Program Publisher, Program Install.date and Program install.path properties and stored each of them in a separate string array.我已经阅读了 Program Name、Program Publisher、Program Install.date 和 Program install.path 属性,并将它们中的每一个都存储在一个单独的字符串数组中。 (All arrays with same length: 99) (所有相同长度的数组:99)

I want to list the program informations according to the alphabetical order of Program name.我想按照节目名称的字母顺序列出节目信息。 Is it possible to do that without combining the 4 arrays in one multidimensional array?是否可以在不将 4 个数组组合成一个多维数组的情况下做到这一点? Or should I first combine the arrays to one two dimensional array ?还是我应该首先将数组组合成一个二维数组? If yes how?如果是怎么办?

As I am new in C# I would thank you if you would make a suggestion.由于我是 C# 新手,如果您能提出建议,我会感谢您。 I have read lots of entries but I got more confused.我已经阅读了很多条目,但我变得更加困惑。

    public string[] Programs_WMI_name = new string[99];
    public string[] Programs_WMI_publisher = new string[99];
    public string[] Programs_WMI_installdate = new string[99];
    public string[] Programs_WMI_installlocation = new string[99];

I have tried this but get error:我试过这个但得到错误:

In my public Class在我的公开课上

    public class TagService{
    public string Programs_WMI_Name { get; set; }
    public string Programs_WMI_Publisher { get; set; }
    public string Programs_WMI_Installdate { get; set; }
    public string Programs_WMI_Installlocation { get; set; }
    public List<string> programs = new List<string>(99);
    }

then然后

    for (int i = 0; i < TagService.Programs_WMI_name.Length; i++)
            {
                programs.Add(new TagService
                {
                    Programs_WMI_Name = TagService.Programs_WMI_name[i],
                    Programs_WMI_Publisher = TagService.Programs_WMI_publisher[i],
                    Programs_WMI_Installdate = TagService.Programs_WMI_installdate[i],
                    Programs_WMI_Installlocation = TagService.Programs_WMI_installlocation[i],
                });
            }
            programs = programs.OrderBy(p => p.Programs_WMI_Name).ToList();
    

Do yourself and others a favor and use classes帮自己和别人一个忙并使用课程

public class ProgramInfo
{
    public string Name { get; set; }
    public string Publisher { get; set; }
    public string InstallDate { get; set; }
    public string InstallLocation { get; set; }
}

from arrays从数组

var programs = new List<ProgramInfo>(Programs_WMI_name.Length);
for (int i = 0; i < Programs_WMI_name.Length; i++)
{
    programs.Add(new ProgramInfo
    {
            Name = Programs_WMI_name[i],
            Publisher = Programs_WMI_publisher[i],
            InstallDate = Programs_WMI_installdate[i],
            InstallLocation = Programs_WMI_installlocation[i],
    });
}

better yet fill from query directly更好的是直接从查询中填写

var programs = new List<ProgramInfo>();
foreach (var row in new ManagementObjectSearcher(somequery).Get())
{
    programs.Add(new ProgramInfo
    {
        Name = row["name"],
        Publisher = row["publisher"],
        InstallDate = row["installdate"],
        InstallLocation = row["installlocation"],
    });
}

then inplace (more efficient)然后就地(更有效)

programs.Sort((l, r) => string.Compare(l.Name, r.Name));

or as a copy或作为副本

programs = programs.Orderby(p => p.Name).ToList();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#如何将数据网格中的列按字母顺序排序? - C# How do I sort the columns in a datagrid into alphabetical order? 如何使用c#按字母顺序对文本文件进行排序 - How to sort a text file in alphabetical order using c# 有没有一种简单的方法可以按字母顺序对字符串中的字符进行排序 - Is there a simple way that I can sort characters in a string in alphabetical order 如何在C#中按升序对这些对象数组进行排序? - How do I sort these arrays of objects in ascending order in C#? 如何在C#中按字母顺序打印字符串中出现的字符? - How to print occurrence of a character in string in alphabetical order in C#? 我如何在c#中对字符串进行排序? - How i can sort string in c#? 如何按工作日的顺序排序(如在日历周中)而不是字母顺序(在C#中)? - How to sort by order of the weekdays (as in a calendar week) instead of alphabetical order (in C#)? 在C#中,如何通过单独的int数组对对象集合进行排序? - In C#, how can I sort a collection of objects by a seperate int array? 在C#中,我如何基于第二列而不是仅按字母顺序的值以升序对锯齿状和二维矩形数组进行排序? - In C# how could I sort a jagged and 2d rectangular array based on 2nd column in ascending order, based on values not just alphabetical order? C#比较两个列表是否具有相同的项目顺序(按字母顺序) - C# Comparing if two lists have the same order of items (alphabetical)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM