sorting

Java: sorting an array with lambda expression?

百般思念 提交于 2021-02-07 09:16:11
问题 I've recently got into functional programming and Java 8 lambdas. I have an array of ints and I want to sort it in an ascending order. The way I am trying to do this with lambda is as follows: Arrays.stream(intArray).sorted((x, y) -> Integer.compare(x, y) == -1); The issue with this is that my compiler says: Error:(12, 32) java: method sorted in interface java.util.stream.IntStream cannot be applied to given types; required: no arguments found: (x,y)->Int[...]== -1 reason: actual and formal

How to sort a list of inter-linked tuples?

穿精又带淫゛_ 提交于 2021-02-07 08:56:04
问题 lst = [(u'course', u'session'), (u'instructor', u'session'), (u'session', u'trainee'), (u'person', u'trainee'), (u'person', u'instructor'), (u'course', u'instructor')] I've above list of tuple, I need to sort it with following logic.... each tuple's 2nd element is dependent on 1st element, e.g. (course, session) -> session is dependent on course and so on.. I want a sorted list based on priority of their dependency, less or independent object will come first so output should be like below,

How to sort a list of inter-linked tuples?

青春壹個敷衍的年華 提交于 2021-02-07 08:55:42
问题 lst = [(u'course', u'session'), (u'instructor', u'session'), (u'session', u'trainee'), (u'person', u'trainee'), (u'person', u'instructor'), (u'course', u'instructor')] I've above list of tuple, I need to sort it with following logic.... each tuple's 2nd element is dependent on 1st element, e.g. (course, session) -> session is dependent on course and so on.. I want a sorted list based on priority of their dependency, less or independent object will come first so output should be like below,

Javascript: sorting an array of objects by three values

跟風遠走 提交于 2021-02-07 08:54:46
问题 I have an array of objects that I would like to sort using the .sort() function. It shall be sorted by three values (first by the first value, then by the second and finally by the third). I have tried something like the following but it doesn't seem to work properly. myArray.sort(function(a,b) { if (a.Value1 === b.Value1) { if (a.Value2 === b.Value2) { return (a.Value3 < b.Value3) ? -1 : (a.Value3 > b.Value3) ? 1 : 0; } else { return (a.Value2 < b.Value2) ? -1 : 1; } } else { if (a.Value2 ==

How to sort a list of inter-linked tuples?

家住魔仙堡 提交于 2021-02-07 08:53:50
问题 lst = [(u'course', u'session'), (u'instructor', u'session'), (u'session', u'trainee'), (u'person', u'trainee'), (u'person', u'instructor'), (u'course', u'instructor')] I've above list of tuple, I need to sort it with following logic.... each tuple's 2nd element is dependent on 1st element, e.g. (course, session) -> session is dependent on course and so on.. I want a sorted list based on priority of their dependency, less or independent object will come first so output should be like below,

Javascript: sorting an array of objects by three values

混江龙づ霸主 提交于 2021-02-07 08:53:43
问题 I have an array of objects that I would like to sort using the .sort() function. It shall be sorted by three values (first by the first value, then by the second and finally by the third). I have tried something like the following but it doesn't seem to work properly. myArray.sort(function(a,b) { if (a.Value1 === b.Value1) { if (a.Value2 === b.Value2) { return (a.Value3 < b.Value3) ? -1 : (a.Value3 > b.Value3) ? 1 : 0; } else { return (a.Value2 < b.Value2) ? -1 : 1; } } else { if (a.Value2 ==

how can I maintain sequence of my list using set?

徘徊边缘 提交于 2021-02-07 08:16:45
问题 In [1]: l1 = ['a',2,3,0,9.0,0,2,6,'b','a'] In [2]: l2 = list(set(l1)) In [3]: l2 Out[3]: ['a', 0, 2, 3, 6, 9.0, 'b'] Here you can see the the list l2 is falling with different sequence then the original l1, I need to remove the duplicate elements from my list without changing the sequence/order of the list elements.... 回答1: If you are not concerned with efficiency, this is O(n*m) >>> sorted(set(l1), key=l1.index) ['a', 2, 3, 0, 9.0, 6, 'b'] Using an intermediate dict is more complicated, but

Sorting numeric String interval in java

你说的曾经没有我的故事 提交于 2021-02-07 07:49:50
问题 I'm having an Person class with some Person and there details as there name, age band. The ageband interval is {"0-5", "6-10", "11-30","31-45", "46-50","50-100", "100-110"}; I'm having a Person class with name , ageBand String interval and it's parameterised constructor, getters, setters. class Person { String name; String ageBand; //say it is string "0-50" which i pass in constructor while creating a person. //getters //setters } class TestAgeBand { public static void main(String args[]) {

How can i sort large csv file without loading to memory

拜拜、爱过 提交于 2021-02-07 05:27:22
问题 I have 20GB+ csv file like this: **CallId,MessageNo,Information,Number** 1000,1,a,2 99,2,bs,3 1000,3,g,4 66,2,a,3 20,16,3,b 1000,7,c,4 99,1,lz,4 ... I must order this file by CallId and MessageNo as asc. (One way is load database->sort->export) How can i sort this file without loading all lines to memory in c#? (like line by line using streamreader) Do you know a library for solution? i wait your advice, thanks 回答1: You should use OS sort commands. Typically it's just sort myfile followed by

Difference in Space Complexity of different sorting algorithms

大憨熊 提交于 2021-02-07 04:16:41
问题 I am trying to understand Space Complexities of different sorting algorithms. http://bigocheatsheet.com/?goback=.gde_98713_member_241501229 from the above link I found that the complexity of bubble sort,insertion and selection sort is O(1) where as quick sort is O(log(n)) and merge sort is O(n). we were actually not allocating extra memory in any of the algorithms. Then why the space complexities are different when we are using the same array to sort them? 回答1: When you run code, memory is