sorted

java8 stream多字段排序,以及空/Null字段排序与分组

感情迁移 提交于 2020-01-13 19:00:53
很多情况下sql不好解决的多表查询,临时表分组,排序,尽量用java8新特性stream进行处理 使用java8新特性,下面先来点基础的 https://www.cnblogs.com/codecat/p/10873757.html List<类> list; 代表某集合 //返回 对象集合以类属性一升序排序 list.stream().sorted(Comparator.comparing(类::属性一)); //返回 对象集合以类属性一降序排序 注意两种写法 list.stream().sorted(Comparator.comparing(类::属性一).reversed());//先以属性一升序,结果进行属性一降序 list.stream().sorted(Comparator.comparing(类::属性一,Comparator.reverseOrder()));//以属性一降序 //返回 对象集合以类属性一升序 属性二升序 list.stream().sorted(Comparator.comparing(类::属性一).thenComparing(类::属性二)); //返回 对象集合以类属性一降序 属性二升序 注意两种写法 list.stream().sorted(Comparator.comparing(类::属性一).reversed()

How can I order a map by value efficiently?

梦想的初衷 提交于 2020-01-04 04:09:08
问题 Consider a std::map<K,V> . I want to re-order the map by value profiting by an appropriate container std::C<V*> or std::C<V&> , in a way that no copies of values are done to store the elements in C. Furthermore, elements in C must be sorted according to the result of int f(V&) applied to each element. Despite my efforts I could not find an appropriate C and an enough efficient way to build it. Do you have any solution? A small example would be much appreciated. 回答1: Seems simple enough. std:

Data structure for efficiently returning the top-K entries of a hash table (map, dictionary)

家住魔仙堡 提交于 2020-01-01 09:23:08
问题 Here's a description: It operates like a regular map with get , put , and remove methods, but has a getTopKEntries(int k) method to get the top-K elements, sorted by the key: For my specific use case, I'm adding, removing, and adjusting a lot of values in the structure, but at any one time there's approximately 500-1000 elements; I want to return the entries for the top 10 keys efficiently. I call the put and remove methods many times. I call the getTopKEntries method. I call the put and

How to sort an ArrayList with object using stream().sorted()

断了今生、忘了曾经 提交于 2019-12-30 09:56:47
问题 I am having real trouble with using stream and sorted to sort my ArrayList and hope someone can help out. The code uses Croatian words, but I don't think that will be a problem for someone who understands what I mean. This is the ArrayList ArrayList<Publikacija> listaPublikacija = new ArrayList<>(); listaPublikacija.add(prvaKnjiga); listaPublikacija.add(drugaKnjiga); listaPublikacija.add(prviCasopis); listaPublikacija.add(drugiCasopis); In my assignment I am supposed to sort these objects

fastest way to determine if an element is in a sorted array

放肆的年华 提交于 2019-12-30 06:32:56
问题 I have an array of sorted ints with a 1,000 or more values (could be up to 5000+). I need to write a function that receives an int and returns a bool based on the element being in the array. I know I can write a for loop with a break, I know I can use jquery .InArray. What would be the best way to implement this, KNOWING that the array is sorted. Thanks. 回答1: Knowing that the array is sorted a binary search would be the best approach. 回答2: I think you'd want to use a binary search routine. A

merging two sorted linked lists into one linked list in python

故事扮演 提交于 2019-12-30 05:18:10
问题 here is my code: def merge_lists(head1, head2): if head1 is None and head2 is None: return None if head1 is None: return head2 if head2 is None: return head1 if head1.value < head2.value: temp = head1 else: temp = head2 while head1 != None and head2 != None: if head1.value < head2.value: temp.next = head1 head1 = head1.next else: temp.next = head2 head2 = head2.next if head1 is None: temp.next = head2 else: temp.next = head1 return temp pass the problem here is stucked in the infinite loop

Light OJ 1089 Points in Segments (II)

醉酒当歌 提交于 2019-12-26 18:19:43
http://www.lightoj.com/volume_showproblem.php?problem=1089 题意很简单,就是给出一些线段和一些点,求每个点被覆盖的次数。做的时候,二分写烂了,一直超时。晕~~~ AC Code 1 #include <cstdio> 2 #include <algorithm> 3 using namespace std; 4 #define lson l,m,rt<<1 5 #define rson m+1,r,rt<<1|1 6 #define maxn 50005 7 struct node{ 8 int cnt; 9 }setree[maxn<<2]; 10 struct op{ 11 int l,r; 12 }mes[maxn]; 13 int sorted[maxn],num[maxn]; 14 void build(int l,int r,int rt) 15 { 16 setree[rt].cnt=0; 17 if(l==r) 18 return; 19 int m=(l+r)>>1; 20 build(lson); 21 build(rson); 22 } 23 int binsearch1(int l,int r,int num) 24 { 25 int m=(l+r)>>1; 26 if(sorted[m-1]<num

Trying to output the x most common words in a text file

最后都变了- 提交于 2019-12-25 07:12:10
问题 I'm trying to write a program that will read in a text file and output a list of most common words (30 as the code is written now) along with their counts. so something like: word1 count1 word2 count2 word3 count3 ... ... ... ... wordn countn in order of count1 > count2 > count3 >... >countn. This is what I have so far but I cannot get the sorted function to perform what I want. The error I get now is: TypeError: list indices must be integers, not tuple I'm new to python. Any help would be

How to order by key (alphabetically) in defaultdict(list) for an inverted index

人走茶凉 提交于 2019-12-24 19:29:08
问题 I have an inverted index. It consists of my word dictionary and the posting list of documents in which the terms appear. What I simply want is to sort my dictionary alphabetically. This is how it looks right now (example): self.index = defaultdict(<type 'list'>, { 'all': [['d03', array('I', [32L, 40L)], ['d07', array('I', [32L, 40L, 47L])], ['d05', array('I', [32L, 40L, 47L])]], 'just': [['d03', array('I', [11L])], ['d07', array('I', [11L])], ['d05', array('I', [11L])], ['d08', array('I',

Python: How to sort a list of objects using attrgetter with case insensitivity

∥☆過路亽.° 提交于 2019-12-24 18:00:10
问题 self.data = sorted(self.data, key=attrgetter('word')) self.data is a list of Word objects. Word objects have 'word', 'definition', 'example' and 'difficulty' attributes. I want to sort by the 'word' strings of each Word object, and the code above does that except it's not case insensitive. How would I go about making the sorting case insensitive? I've tried the solutions from another question asked here, but when I tried it, it said "TypeError: 'Word' object is not subscriptable". What could