sorting

Sorting a list in python from the second element on

早过忘川 提交于 2021-02-07 19:16:14
问题 I want to sort a list but I want it to be sorted excluding the first element. For example: a = ['T', 4, 2, 1, 3] Now I want the list to be sorted but the first element should stay in its place: a = ['T', 1, 2, 3, 4] I know this can be done by using a sorting algorithm but is there a one line way to do it or a more pythonic way to do this? 回答1: You could slice it, sort the trailing slice and concatenate it afterwards: >>> a = a[:1] + sorted(a[1:]) >>> a ['T', 1, 2, 3, 4] 回答2: You can do so by

How to sort this lua table?

蓝咒 提交于 2021-02-07 19:05:36
问题 I have next structure self.modules = { ["Announcements"] = { priority = 0, -- Tons of other attributes }, ["Healthbar"] = { priority = 40, -- Tons of other attributes }, ["Powerbar"] = { priority = 35, -- Tons of other attributes }, } I need to sort this table by priorty DESC, other values does not matter. E.g. Healthbar first, then Powerbar, and then going all others. // edit. Keys must be preserved. // edit #2 Found a solution, thanks you all. local function pairsByPriority(t) local

How to sort this lua table?

梦想的初衷 提交于 2021-02-07 19:05:07
问题 I have next structure self.modules = { ["Announcements"] = { priority = 0, -- Tons of other attributes }, ["Healthbar"] = { priority = 40, -- Tons of other attributes }, ["Powerbar"] = { priority = 35, -- Tons of other attributes }, } I need to sort this table by priorty DESC, other values does not matter. E.g. Healthbar first, then Powerbar, and then going all others. // edit. Keys must be preserved. // edit #2 Found a solution, thanks you all. local function pairsByPriority(t) local

sorting strings in Java based on substrings

只谈情不闲聊 提交于 2021-02-07 18:43:44
问题 I have a list of strings def123, abc999, zzz000, abc123, zzz111 . I want the list sorted such that first three characters are sorted in ascendng order and next three in descending. So the output should be abc999, abc123, def123, zzz111,zzz000 Is this possible? 回答1: Other answers have suggested you implement Comparator . That's no longer necessary with recent utility methods added to the interface in Java 8: list.sort(Comparator .comparing(s -> s.substring(0, 3)) .thenComparing(s -> s.subtring

Sort matrix based on its diagonal entries

社会主义新天地 提交于 2021-02-07 17:47:10
问题 First of all I would like to point out that my question is different than this one: Sort a numpy matrix based on its diagonal The question is as follow: Suppose I have a numpy matrix A= 5 7 8 7 2 9 8 9 3 I would like to sort the matrix based on its diagonal and then re-arrange the matrix element based on it. Such that now sorted_A: 2 9 7 9 3 8 7 8 5 Note that: (1). The diagonal is sorted (2). The other elements (non-diagonal) re-adjusted by it. How? because diag(A)= [5,2,3] & diag(sorted_A)=

Java Arrays.sort() method takes 1D arrays, but I can pass a 2D array as well then why can't I do int[] a=b(where b is a 2D array)?

断了今生、忘了曾经 提交于 2021-02-07 14:58:16
问题 I'm having difficulty understanding the concept that I can pass a 2D array to java's Arrays.sort() method.(I have seen the java docs, the sort method can take only 1D arrays) But then when I try the following code I get error int[][] b={{1,2},{2,3}}; int[] a=b; But the following code works fine int[][] temp = { { 1, 50, 5 }, { 2, 30, 8 }, { 3, 90, 6 },{ 4, 20, 7 }, { 5, 80, 9 }, }; Arrays.sort(temp, new Comparator<int[]>() { @Override public int compare(int[] o1, int[] o2) { return Integer

Sort two arrays of different values maintaining original pairing

爱⌒轻易说出口 提交于 2021-02-07 14:16:51
问题 I have two js arrays, one contains strings, the other color codes, something like: strings = ['one', 'twooo', 'tres', 'four']; colors = ['000000', 'ffffff', 'cccccc', '333333']; I need to sort the first array by the length of the values, longer first. I know I can do something like: strings.sort(function(a, b){ return b.length - a.length; }); But this way I am losing the color assined to each string. How can I sort both arrays keeping the keys pairing? 回答1: Blatantly copied from Sorting with

Sorting a stack in ascending order in C, using two stacks

ぐ巨炮叔叔 提交于 2021-02-07 13:56:15
问题 I was given a task to sort numbers in stack a of integers in ascending order using two stacks a and b . Using eleven operations: sa : swap a - swap the first 2 elements at the top of stack a sb : swap b - swap the first 2 elements at the top of stack b . ss : sa and sb at the same time. pa : push a - take the first element at the top of b and put it at the top of a . pb : push b - take the first element at the top of a and put it at the top of b . ra : rotate a - shift up all elements of

Sorting a stack in ascending order in C, using two stacks

ぐ巨炮叔叔 提交于 2021-02-07 13:56:07
问题 I was given a task to sort numbers in stack a of integers in ascending order using two stacks a and b . Using eleven operations: sa : swap a - swap the first 2 elements at the top of stack a sb : swap b - swap the first 2 elements at the top of stack b . ss : sa and sb at the same time. pa : push a - take the first element at the top of b and put it at the top of a . pb : push b - take the first element at the top of a and put it at the top of b . ra : rotate a - shift up all elements of

How do I sort jagged array by row in C#?

陌路散爱 提交于 2021-02-07 12:28:29
问题 I have 2D jagged array. And I want to sort it by any rows. I've searched and found code for sorting by columns private static void Sort<T>(T[][] data, int col) { Comparer<T> comparer = Comparer<T>.Default; Array.Sort<T[]>(data, (x,y) => comparer.Compare(x[col],y[col])); } Can I adapt it for sort by any rows ? Any help is appreciated. Sample of my jagged array (Added) using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication3 { class