sorting

php sort array by $array[*][sum of this]

时光怂恿深爱的人放手 提交于 2021-01-28 23:12:36
问题 I have a multidimensional array which stores information about players in a game. It stores an array for each player, and each player is an array of information. One of the values in each player is an array of scores. I want to output my data to a table in order of highest total score to lowest. At the moment, the output is in no particular order. The only way I know of to get the total score is array_sum($allplayers[0][2]) or similar. How can I sort the array $allplayers so that when I loop

Sort ArrayList with two dimensional objects

落花浮王杯 提交于 2021-01-28 21:07:10
问题 I have an ArrayList where I store 10 objects with two random generated numbers each and I want to sort the list by the Y values of the objects. Random rndm = new Random(); ArrayList CustomList = new ArrayList(); for (int i = 0; i < 10; i++) { Point LP = new Point(rndm.Next(50), rndm.Next(50)); CustomList.Add(LP); } PrintList(CustomList); CustomList.Sort(); PrintList(CustomList); The Sort method throws the following exception: System.InvalidOperationException: The comparer threw an exception.

How to plot data on a cylindrical grid using slice?

此生再无相见时 提交于 2021-01-28 20:46:24
问题 I have input data on a cylindrical grid and want to plot them using slice in MATLAB. To do this I first transform the reference coordinates into Cartesian coordinates using pol2cart . r = linspace(1,4,4); phi = linspace(0,2*pi,10); z = linspace(1,3,3); [rmesh,phimesh,zmesh]=meshgrid(r,phi,z) [xmesh,ymesh,zmesh]=pol2cart(phimesh,rmesh,zmesh) When I use slice now (e.g. slice(xmesh,ymesh,zmesh,ones(10,4,3),2,2,2) ) an error is thrown, because the coordinate matrices are not ordered correctly (

How to sort only part of array? between given indexes

天涯浪子 提交于 2021-01-28 19:46:37
问题 I have an array of numbers and I need to sort only part of it by left and right barriers. Can you please help me how to implement it correctly? const sortBetween = (arr, left, right) => { ... } given: [9,3,5,7,4,9,4,1] , left = 2, right = 5 expected: [9,3,4,5,7,9,4,1] [9,3, 5,7,4,9 ,4,1] -> [9,3, 4,5,7,9 ,4,1] Thanks for the help. 回答1: This is an approach by using sort directly, but shaping the access with a Proxy for length and the indices. type original array length ------- ----------------

Sorting by Subtraction

和自甴很熟 提交于 2021-01-28 19:21:08
问题 So I was working on this sorting algorithm in java, and was wondering if anybody has seen anything like this before. Specifically, this algorithm is meant to sort very large lists with a very small range of numbers. If you have seen that or have any suggestions to enhance the algorithm, can you say something about that below? I have the code here: public static int[] sort(int[] nums) { int lowest = Integer.MAX_VALUE; for (int n : nums) { if (n < lowest) lowest = n; } int index = 0; int down =

randomly sort a list with bias

强颜欢笑 提交于 2021-01-28 19:11:19
问题 I have a list as follows: i = [ {'id': '1', 'P': 0.5}, {'id': '2', 'P': 0.4}, {'id': '3', 'P': 0.8}, ... {'id': 'x', 'P': P(x)} ] When I do the following: i.sort(key=lambda x: x['P'], reverse=True) The list gets sorted based on P where the element with the largest P is in front. but what if I want to make it seem randomized so that even a element with a small P value (with very small probability) can be the first item in the list? Is it possible to implement that using the sort() function or

PHP sorting array values from within an array

若如初见. 提交于 2021-01-28 19:01:30
问题 I have an array where the Key is the sales person. The value of the key is an array, with 2 values: A percentage value, and a total value. In a nutshell, I am trying to sort the Highest percentage value on top. $sales = array( "Johnny Smith" => array(75.25,45), "Ash Han" => array(55.67, 99), "Janice K." => array(90.00, 40) ); I've tried a few arrangements or array sorting functions, and none are good enough to even display, since they don't have examples where the array value is an array to

MongoDB custom sorting

孤人 提交于 2021-01-28 18:54:26
问题 i have a collection of records as follows { "_id":417, "ptime":ISODate("2013-11-26T11:18:42.961Z"), "p":{ "type":"1", "txt":"test message" }, "users":[ { "uid":"52872ed59542f", "pt":ISODate("2013-11-26T11:18:42.961Z") }, { "uid":"524eb460986e4", "pt":ISODate("2013-11-26T11:18:42.961Z") }, { "uid":"524179060781e", "pt":ISODate("2013-11-27T12:48:35Z") } ], }, { "_id":418, "ptime":ISODate("2013-11-25T11:18:42.961Z"), "p":{ "type":"1", "txt":"test message 2" }, "users":[ { "uid":"524eb460986e4",

How to sort a tuple based on a value within the list of tuples

允我心安 提交于 2021-01-28 16:42:28
问题 In python, I wish to sort tuples based on the value of their last element. For example, i have a tuple like the one below. tuples = [(2,3),(5,7),(4,3,1),(6,3,5),(6,2),(8,9)] which after sort I wish to be in this format. tuples = [(4,3,1),(6,2),(2,3),(6,3,5),(5,7),(8,9)] How do i get to doing that? 回答1: Povide list.sort with an appropriate key function that returns the last element of a tuple: tuples.sort(key=lambda x: x[-1]) 回答2: You can use: from operator import itemgetter tuples = sorted

Sorting Pandas dataframe data within Groupby groups

半腔热情 提交于 2021-01-28 15:31:28
问题 I have a large pandas dataframe that can be represented structurally as: id date status 0 12 2015-05-01 0 1 12 2015-05-22 1 2 12 2015-05-14 1 3 12 2015-05-06 0 4 45 2015-05-03 1 5 45 2015-05-12 1 6 45 2015-05-02 0 7 51 2015-05-05 1 8 51 2015-05-01 0 9 51 2015-05-23 1 10 51 2015-05-17 1 11 51 2015-05-03 0 12 51 2015-05-05 0 13 76 2015-05-04 1 14 76 2015-05-22 1 15 76 2015-05-08 0 And can be created in Python 3.4 using: tempDF = pd.DataFrame({ 'id': [12,12,12,12,45,45,45,51,51,51,51,51,51,76,76