sorting

ArrayList.Sort should be a stable sort with an IComparer but is not?

|▌冷眼眸甩不掉的悲伤 提交于 2020-08-06 11:41:07
问题 A stable sort is a sort that maintains the relative ordering of elements with the same value. The docs on ArrayList.Sort say that when an IComparer is provided the sort is stable: If comparer is set to null, this method performs a comparison sort (also called an unstable sort); that is, if two elements are equal, their order might not be preserved. In contrast, a stable sort preserves the order of elements that are equal. To perform a stable sort, you must implement a custom IComparer

Sort by date in mongoose aggregation framework

拜拜、爱过 提交于 2020-08-01 09:58:27
问题 Im working on a nodejs+mongodb project using mongoose. Now I have come across a question I don't know the answer to. I am using aggregation framework to get grouped results. The grouping is done on a date excluding time data field like: "2013 02 06". Code looks like this: MyModel.aggregate([ {$match: {$and: [{created_date: {$gte: start_date}}, {created_date: {$lte: end_date}}]}}, {$group: { _id: { year: {$year: "$created_at"}, month: {$month: "$created_at"}, day: {$dayOfMonth: "$created_at"}

How to sort one list based on another? [duplicate]

你说的曾经没有我的故事 提交于 2020-08-01 05:31:51
问题 This question already has answers here : how to keep elements of a list based on another list (3 answers) Closed 7 months ago . I have two list, one reference and one input list Ref = [3, 2, 1, 12, 11, 10, 9, 8, 7, 6, 5, 4] Input = [9, 5, 2, 3, 10, 4, 11, 8] I want to sort Input list, in the order as that of Ref. If some element is missing in Input list, it can skip and go for the other element. Hence sorted Input list, based on Ref list will be like this Sorted_Input = [3, 2, 11, 10, 9, 8, 5

Sorting objects based on property value in D3

╄→尐↘猪︶ㄣ 提交于 2020-08-01 02:52:26
问题 I have a array of objects for use in D3 e.g var cities = [ { city: "London", country: "United Kingdom", index: 280 }, { city: "Geneva", country: "Switzerland", index: 259 }, { city: "New York City", country: "United States", index: 237 }, { city: "Singapore", country: "Singapore", index: 228 }, { city: "Paris", country: "France", index: 219 }, { city: "San Francisco", country: "United States", index: 218 }, { city: "Copenhagen", country: "Denmark", index: 217 }, { city: "Sydney", country:

Sort a list of dicts by each dicts key

不问归期 提交于 2020-07-30 18:52:24
问题 I am creating a list of dicts, then I want to sort the dicts in the list by the value of the key, lowest to highest. Everything works except the sort: def pythagorean(x1, y1, x2=0, y2=0): return ((x1 - x2)**2 + (y1 - y2)**2)**0.5 points = [(-2, -4), (0, -2), (-1, 0), (3, -5), (-2, -3), (3, 2)] dicts = [] for coord in points: d = {} a1, b1 = coord distance = pythagorean(a1, b1) d[distance] = (a1, b1) dicts.append(d) for i in dicts: print(i) dist_list = [] for item in dicts: for key in item:

how to sort List<T> in c# / .net

。_饼干妹妹 提交于 2020-07-30 04:32:18
问题 I have a class PropertyDetails : public class PropertyDetails { public int Sequence { get; set; } public int Length { get; set; } public string Type { get; set; } } I am creating a list of PropertyDetails as List<PropertyDetails> propertyDetailsList=new List<PropertyDetails>(); I want to sort this list by PropertyDetails.Sequence . Linq solutions are welcome. 回答1: If you want to sort the existing list in-place then you can use the Sort method: List<PropertyDetails> propertyDetailsList = ...

Why would sort fail?

我只是一个虾纸丫 提交于 2020-07-29 11:50:53
问题 I'm writing a library routine, which among other things will do some rather complex sorting of nested arrays. I see from the documentation that all the array sort function (including the ones using built-in comparators) can return false on failure - but when would this ever be the case??? 回答1: It would fail when the variable you send to the function is NOT an array Example: asort('Hello');//fails asort(array(1,2,35,7,2,8,3));//true 回答2: I also stumbled upon this question and did some research

Why would sort fail?

假装没事ソ 提交于 2020-07-29 11:49:31
问题 I'm writing a library routine, which among other things will do some rather complex sorting of nested arrays. I see from the documentation that all the array sort function (including the ones using built-in comparators) can return false on failure - but when would this ever be the case??? 回答1: It would fail when the variable you send to the function is NOT an array Example: asort('Hello');//fails asort(array(1,2,35,7,2,8,3));//true 回答2: I also stumbled upon this question and did some research

Sorting order items by SKU in Woocommerce

人盡茶涼 提交于 2020-07-25 06:39:13
问题 I am trying to order the products by sku within an order in an email in Woocommerce . I have not had any luck with the following code. Some help? Thanks since now! add_filter( 'woocommerce_order_get_items', function( $items, $order ) { uasort( $items, function( $a, $b ) { return strnatcmp( $a['_sku'], $b['_sku'] ); } ); return $items; }, 10, 2 ); Sample sort result: INFSTRAW I NFMUFFIN INFFLORES INFTAFLOR INFTAPINK CTEPINK4 CTECAKE4 INFCHOCO UCUBTOMA 回答1: Updated On July 2020 Here is the way

Sorting order items by SKU in Woocommerce

对着背影说爱祢 提交于 2020-07-25 06:38:11
问题 I am trying to order the products by sku within an order in an email in Woocommerce . I have not had any luck with the following code. Some help? Thanks since now! add_filter( 'woocommerce_order_get_items', function( $items, $order ) { uasort( $items, function( $a, $b ) { return strnatcmp( $a['_sku'], $b['_sku'] ); } ); return $items; }, 10, 2 ); Sample sort result: INFSTRAW I NFMUFFIN INFFLORES INFTAFLOR INFTAPINK CTEPINK4 CTECAKE4 INFCHOCO UCUBTOMA 回答1: Updated On July 2020 Here is the way