sort

Sort string array by element length

匿名 (未验证) 提交于 2019-12-03 03:10:03
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Having an array of strings how can I update it so its elements are sorted by its length. I was trying string[] arr = {"aa","ss","a","abc"}; arr = arr.OrderBy(aux => aux.Length); So, I would get a,aa,ss,abc , but it says cannot implicitly convert type 'system.linq.iorderedenumerable to string[]' So, I was doing foreach (string s in arr.OrderBy(str => str.Length)) { // } Is there other way to do this? 回答1: Since arr is an array, you can use the convenient Array.Sort method: Array.Sort(arr, (x, y) => x.Length.CompareTo(y.Length)); foreach

Ruby on Rails: how do I sort with two columns using ActiveRecord?

匿名 (未验证) 提交于 2019-12-03 03:08:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I want to sort by two columns, one is a DateTime ( updated_at ), and the other is a Decimal (Price) I would like to be able to sort first by updated_at, then, if multiple items occur on the same day, sort by Price. 回答1: Assuming you're using MySQL, Model.all(:order => 'DATE(updated_at), price') Note the distinction from the other answers. The updated_at column will be a full timestamp, so if you want to sort based on the day it was updated, you need to use a function to get just the date part from the timestamp. In MySQL, that is DATE() .

Sorting a vector of (double precision) reals and obtain their order

匿名 (未验证) 提交于 2019-12-03 03:08:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: in C++ would like to sort a lengthy (2^20) vector of reals, obviously sort() does the trick. Having used R before I was used to the nice order() function which yields the permutation that leads to the sorted vector. Probably someone has done this in C++, maybe it's just my weak google-Fu that prevents me from finding it. And yeah, obivously my C++ newbness could stop me from spotting something straightforward. Example: x = {24, 55, 22, 1} then the permutation perm = {3, 2, 0, 1} maps the original x to the sorted x in ascending

How do I sort a tab separated file on the nth column using cygwin sort?

匿名 (未验证) 提交于 2019-12-03 03:06:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I have a huge tab separated file which I want to sort on its 2nd column. I need to use the tab character as the field delimiter in cygwin sort. So I need something like this: sort - t \t - k 2 , 2 in . txt > out . txt But the command prompt evaluates '\t' literally and not as the tab character. Note that I need to do this on a Windows machine running Cygwin. Variations such as sort - t "\t" sort - t \"\t\" don't work, neither does putting this in a cmd file with an actual tab in place of the \t above. Edit: A solution using either

How to sort dataframe in R with specified column order preservation?

匿名 (未验证) 提交于 2019-12-03 03:06:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Let's say I have a data.frame x <- data.frame(a = c('A','A','A','A','A', 'C','C','C','C', 'B','B','B'), b = c('a','c','a','a','c', 'd', 'e','e','d', 'b','b','b'), c = c( 7, 3, 2, 4, 5, 3, 1, 1, 5, 5, 2, 3), stringsAsFactors = FALSE) > x a b c 1 A a 7 2 A c 3 3 A a 2 4 A a 4 5 A c 5 6 C d 3 7 C e 1 8 C e 1 9 C d 5 10 B b 5 11 B b 2 12 B b 3 I would like to sort x by columns b and c but keeping order of a as before. x[order(x$b, x$c),] - breaks order of column a. This is what I want: a b c 3 A a 2 4 A a 4 1 A a 7 2 A c 3 5 A c 5 6 C d 3 9 C d

How to remove duplicates from a file and write to the same file?

匿名 (未验证) 提交于 2019-12-03 03:06:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I know my title is not much self-explanatory but let me try to explain it here. I have a file name test.txt which has some duplicate lines. Now, what I want to do is remove those duplicate lines and at the same time update test.txt with the new content. test.txt AAAA BBBB AAAA CCCC I know I can use sort -u test.txt to remove the duplicates but to update the file with new content how do I redirect it's output to the same file. The below command doesn't work. sort -u test.txt > test.txt So, why the above command is not working and whats the

324. Wiggle Sort II

倾然丶 夕夏残阳落幕 提交于 2019-12-03 03:05:18
Given an unsorted array nums , reorder it such that nums[0] < nums[1] > nums[2] < nums[3]... . Example 1: Input: nums = [1, 5, 1, 1, 6, 4] Output: One possible answer is [1, 4, 1, 5, 1, 6]. Example 2: Input: nums = [1, 3, 2, 2, 3, 1] Output: One possible answer is [2, 3, 1, 3, 1, 2]. Note: You may assume all input has valid answer. Follow Up: Can you do it in O(n) time and/or in-place with O(1) extra space? class Solution { public void wiggleSort(int[] nums) { if (nums.length <= 1) { return; } Arrays.sort(nums); int mid = 0; if (nums.length % 2 == 0) { mid = nums.length / 2; } else { mid =

How to use std::sort with a vector of structures and compare function?

匿名 (未验证) 提交于 2019-12-03 03:05:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Thanks for a solution in C , now I would like to achieve this in C++ using std::sort and vector: typedef struct { double x; double y; double alfa; } pkt; vector wektor; filled up using push_back(); compare function: int porownaj(const void *p_a, const void *p_b) { pkt *pkt_a = (pkt *) p_a; pkt *pkt_b = (pkt *) p_b; if (pkt_a->alfa > pkt_b->alfa) return 1; if (pkt_a->alfa alfa) return -1; if (pkt_a->x > pkt_b->x) return 1; if (pkt_a->x x) return -1; return 0; } sort(wektor.begin(), wektor.end(), porownaj); // this makes loads of errors on

How to use std::sort with a vector of structures and compare function?

匿名 (未验证) 提交于 2019-12-03 03:04:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Thanks for a solution in C , now I would like to achieve this in C++ using std::sort and vector: typedef struct { double x; double y; double alfa; } pkt; vector wektor; filled up using push_back(); compare function: int porownaj(const void *p_a, const void *p_b) { pkt *pkt_a = (pkt *) p_a; pkt *pkt_b = (pkt *) p_b; if (pkt_a->alfa > pkt_b->alfa) return 1; if (pkt_a->alfa alfa) return -1; if (pkt_a->x > pkt_b->x) return 1; if (pkt_a->x x) return -1; return 0; } sort(wektor.begin(), wektor.end(), porownaj); // this makes loads of errors on

Range-based for with brace-initializer over non-const values?

匿名 (未验证) 提交于 2019-12-03 03:04:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am trying to iterate over a number of std::list s, sorting each of them. This is the naive approach: #include<list> using namespace std; int main(void){ list<int> a,b,c; for(auto& l:{a,b,c}) l.sort(); } producing aa.cpp:5:25: error: no matching member function for call to 'sort' for(auto& l:{a,b,c}) l.sort(); ~~^~~~ /usr/bin/../lib64/gcc/x86_64-linux-gnu/4.9/../../../../include/c++/4.9/bits/stl_list.h:1586:7: note: candidate function not viable: 'this' argument has type 'const std::list<int, std::allocator<int> >', but method is not marked