[英]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.