[英]order an array C# .NET
I have a list of DICOM images and would like to sort by Z order (it's an attribute within the image) 我有一个DICOM图像列表,并想按Z顺序排序(这是图像中的一个属性)
here's my code for adding the images to the List 这是我将图像添加到列表中的代码
private List<DicomImage> img = new List<DicomImage>();
for (int i = 0; i < imagenes.Count; i++) //imagenes is a variable that holds the number of images (coming from an OpenFileDialog)
{
img.Add(new DicomImage(imagenes[i]));
}
Now, how can I order ascending by Z? 现在,如何订购由Z升序?
Let's say Z attribute can be accessed simply by entering something like this: 假设只需输入以下内容即可访问Z属性:
int Z=img[i].Z; int Z = img [i] .Z;
Assuming imagenes
is some sort of IEnumerable<T>
, I'd use LINQ for the whole thing: 假设imagenes
是某种IEnumerable<T>
,那么我将在整个过程中使用LINQ:
var dicomImages = imagenes.Select(original => new DicomImage(original))
.OrderBy(image => image.Z)
.ToList();
If img is a List<T>
, you can use the Sort(Comparison<T>)
method. 如果img是List<T>
,则可以使用Sort(Comparison<T>)
方法。
img.Sort((img1, img2) => img1.Z.CompareTo(img2.Z));
This just tells the sort method that when comparing two images, call this function that just reverts to comparing their Z values for the result. 这只是告诉sort方法,当比较两个图像时,调用此函数,该函数将还原为比较其Z值以获得结果。
building on the comment to John skeets answer here's a more performant variant which saves on copying the images over 以John skeets的评论为基础,这是一个性能更高的变体,它节省了将图像复制到
var dicomImages = ofdmulti.FileNames.Select(filename => new DicomImage(filename))
.OrderBy(image => image.Z);
foreach (var image in dicomImages)
{
// Do Something usefull
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.