如何通过不使用内置排序的属性对类对象进行排序c#[closed](How to sort an array of class objects by a property NOT using in

编程入门 行业动态 更新时间:2024-10-27 03:35:16
如何通过不使用内置排序的属性对类对象进行排序c#[closed](How to sort an array of class objects by a property NOT using in-built sorting c# [closed])

目前,我已经构建了“Element”类的1200个实例的数组。 每一个通过它的构造函数自动填充(从9个不同的数组中逐行扫描数据(数组源自9个不同的文本文件))。

public class Element { public int elementDay; public string elementMonth; public int elementYear; public string elementTime; public int elementTimestamp; public string elementRegion; public float elementLongitude; public float elementLatitude; public float elementMagnitude; public float elementDepth; public float elementIris_Id; public Element(int[] day, string[] month, int[] year, string[] time, int[] timestamp, string[] region, float[] longitude, float[] latitude, float[] magnitude, float[] depth, float[] iris_Id, ref int counter) { elementDay = day[counter]; elementMonth = month[counter]; elementYear = year[counter]; elementTime = time[counter]; elementTimestamp = timestamp[counter]; elementRegion = region[counter]; elementLongitude = longitude[counter]; elementLatitude = latitude[counter]; elementMagnitude = magnitude[counter]; elementDepth = depth[counter]; elementIris_Id = iris_Id[counter]; counter++; } public static void CreateElementList(Element[] arrayElements, int[] day, string[] month, int[] year, string[] time, int[] timestamp, string[] region, float[] longitude, float[] latitude, float[] magnitude, float[] depth, float[] iris_Id, ref int counter) { for (int i = 0; i < 1200; i++) { arrayElements[i] = new Element(day, month, year, time, timestamp, region, longitude, latitude, magnitude, depth, iris_Id, ref counter); } }

现在我需要找到一种按特定属性排序这些对象的方法。 我想要的一个工作示例是: Array.Sort(arrayElements, delegate (Element x, Element y) { return y.elementDay.CompareTo(x.elementDay); });

但是我不能使用它,因为它是一个内置函数。 我试图实现一个Quicksort算法来按'天'对这些对象进行排序,但是我不知道是否可以用一个元素数组来做这样的事情,因为我只知道如何用一个简单的整数数组来做到这一点。 最终目标是能够根据所选属性对任何属性进行排序,并对整个元素数组进行排序。

请记住这是家庭作业,所以请不要给我完整的解决方案,我主要想知道是否有可能/值得我的时间,还是应该尝试另一种方法?

Currently I have built an array of 1200 instances of the 'Element' class. Each one is populated automatically via its constructor (scraping data line by line from 9 different arrays (the arrays originate from 9 different text files)).

public class Element { public int elementDay; public string elementMonth; public int elementYear; public string elementTime; public int elementTimestamp; public string elementRegion; public float elementLongitude; public float elementLatitude; public float elementMagnitude; public float elementDepth; public float elementIris_Id; public Element(int[] day, string[] month, int[] year, string[] time, int[] timestamp, string[] region, float[] longitude, float[] latitude, float[] magnitude, float[] depth, float[] iris_Id, ref int counter) { elementDay = day[counter]; elementMonth = month[counter]; elementYear = year[counter]; elementTime = time[counter]; elementTimestamp = timestamp[counter]; elementRegion = region[counter]; elementLongitude = longitude[counter]; elementLatitude = latitude[counter]; elementMagnitude = magnitude[counter]; elementDepth = depth[counter]; elementIris_Id = iris_Id[counter]; counter++; } public static void CreateElementList(Element[] arrayElements, int[] day, string[] month, int[] year, string[] time, int[] timestamp, string[] region, float[] longitude, float[] latitude, float[] magnitude, float[] depth, float[] iris_Id, ref int counter) { for (int i = 0; i < 1200; i++) { arrayElements[i] = new Element(day, month, year, time, timestamp, region, longitude, latitude, magnitude, depth, iris_Id, ref counter); } }

Right now I need to find a way of sorting these objects by certain properties. A working example of what I want is: Array.Sort(arrayElements, delegate (Element x, Element y) { return y.elementDay.CompareTo(x.elementDay); });

However I cannot use this as it is an in-built function. I'm trying to implement a Quicksort algorithm to sort these objects by 'day' but I do not know if it is possible to do such a thing with an array of elements as I only know how to do this with a simple integer array. The end goal is to be able to sort by any of the properties and for the entire array of elements to be sorted with respect to the chosen property.

Please keep in mind this is homework, so please do not give me the full solution, I mainly want to know if it is possible/worth my time, or should I simply try another approach?

最满意答案

您通常如何实施一种排序框架:

public static void MySort<T>(IList<T> list, Comparison<T> comparison = null) { if (comparison == null) { comparison = Comparer<T>.Default.Compare; } // ** Your sort algorithm goes here ** // To compare two elements do it like: // Compare first and last element int cmp = comparison(list[0], list[list.Count - 1]); if (cmp < 0) { Console.WriteLine("first < last"); } else if (cmp == 0) { Console.WriteLine("first == last"); } else { Console.WriteLine("first > last"); } }

这里没有排序算法,但是您可以看到如何比较两个元素, Array.Sort和类似方法如何正常获取默认Comparer<>以及如何从Comparer<>获取Comparison<T> Comparer<> 。

然后:

var elements = new[] { new Element { elementDay = 15, elementMonth = 3, elementYear = 2017 }, new Element { elementDay = 14, elementMonth = 3, elementYear = 2017 }, new Element { elementDay = 13, elementMonth = 3, elementYear = 2016 }, }; MySort(elements, (x, y) => x.elementDay.CompareTo(y.elementDay));

现在......如果你想按照不同的领域进行排序,链接它们呢? ( elementYear ,然后elementMonth ,然后elementDay为例)?

“链接”比较创建者是(其中之一)解决方案:-)

public class MultiComparison { private MultiComparison() { } // Create a singole comparison from an array of comparisons, // this comparison is the "chaining" of all the comparisons public static Comparison<T> Create<T>(params Comparison<T>[] comparisons) { if (comparisons == null || comparisons.Length == 0) { throw new ArgumentException("comparisons"); } // Single comparison == it is already the comparison we can return if (comparisons.Length == 1) { return comparisons[0]; } // We clone the array so that the caller of Create can't modify it // behind the scene. comparisons = (Comparison<T>[])comparisons.Clone(); // We return a method that will use all the comparisons, // stopping at the first comparison that gives a result != 0 Comparison<T> comparison = (x, y) => { for (int i = 0; i < comparisons.Length; i++) { int cmp = comparisons[i](x, y); if (cmp != 0) { return cmp; } } return 0; }; return comparison; } }

像这样使用它:

MySort( elements, MultiComparison.Create<Element>( (x, y) => x.elementYear.CompareTo(y.elementYear), (x, y) => x.elementMonth.CompareTo(y.elementMonth), (x, y) => x.elementDay.CompareTo(y.elementDay) ));

A skeleton of how you normally implement a sort:

public static void MySort<T>(IList<T> list, Comparison<T> comparison = null) { if (comparison == null) { comparison = Comparer<T>.Default.Compare; } // ** Your sort algorithm goes here ** // To compare two elements do it like: // Compare first and last element int cmp = comparison(list[0], list[list.Count - 1]); if (cmp < 0) { Console.WriteLine("first < last"); } else if (cmp == 0) { Console.WriteLine("first == last"); } else { Console.WriteLine("first > last"); } }

There is no sort algorithm there, but you can see how you can compare two elements, how the default Comparer<> is obtained normally by Array.Sort and similar methods, and how to obtain a Comparison<T> from a Comparer<>.

Then:

var elements = new[] { new Element { elementDay = 15, elementMonth = 3, elementYear = 2017 }, new Element { elementDay = 14, elementMonth = 3, elementYear = 2017 }, new Element { elementDay = 13, elementMonth = 3, elementYear = 2016 }, }; MySort(elements, (x, y) => x.elementDay.CompareTo(y.elementDay));

Now... What if you want to sort by different fields, chaining them? (elementYear, then elementMonth and then elementDay for example)?

A "chained" comparison creator is (one of) the solution(s) :-)

public class MultiComparison { private MultiComparison() { } // Create a singole comparison from an array of comparisons, // this comparison is the "chaining" of all the comparisons public static Comparison<T> Create<T>(params Comparison<T>[] comparisons) { if (comparisons == null || comparisons.Length == 0) { throw new ArgumentException("comparisons"); } // Single comparison == it is already the comparison we can return if (comparisons.Length == 1) { return comparisons[0]; } // We clone the array so that the caller of Create can't modify it // behind the scene. comparisons = (Comparison<T>[])comparisons.Clone(); // We return a method that will use all the comparisons, // stopping at the first comparison that gives a result != 0 Comparison<T> comparison = (x, y) => { for (int i = 0; i < comparisons.Length; i++) { int cmp = comparisons[i](x, y); if (cmp != 0) { return cmp; } } return 0; }; return comparison; } }

Use it like:

MySort( elements, MultiComparison.Create<Element>( (x, y) => x.elementYear.CompareTo(y.elementYear), (x, y) => x.elementMonth.CompareTo(y.elementMonth), (x, y) => x.elementDay.CompareTo(y.elementDay) ));

更多推荐

本文发布于:2023-07-14 23:44:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1108371.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:属性   对象   closed   sort   property

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!