digitalmars.D.learn - Trouble using 'sort'
- Bahman Movaqar (24/24) Jul 25 2016 I have a range which is the result of a couple of chained range
- Bahman Movaqar (7/31) Jul 25 2016 Alright...further experiments. The following works:
- Bahman Movaqar (5/12) Jul 25 2016 I meant
- Jonathan M Davis via Digitalmars-d-learn (11/42) Jul 25 2016 /home/bahman/Programs/D/dmd-2.071.0/linux/bin64/../../src/phobos/std/alg...
- drug (3/6) Jul 26 2016 Another option is `makeIndex` (std.algorithm.sorting) and then sorting
- Bahman Movaqar (5/7) Jul 26 2016 That's an interesting option; at least I don't have to touch the range.
- Bahman Movaqar (4/15) Jul 26 2016 Thanks...that explains it.
I have a range which is the result of a couple of chained range operations, and each element is: Tuple!(string, "product", double, "price") Now I'd like to sort the range by "price" using: sort!((pp1, pp2) => cmp(pp1.price, pp2.price) > 0)(theRange) But I get a compile time error: source/services.d(166,63): Error: template std.algorithm.sorting.sort cannot deduce function from argument types !((pp1, pp2) => cmp(pp1.price, pp2.price) > 0)(MapResult!(__lambda5, Result)), candidates are: /home/bahman/Programs/D/dmd-2.071.0/linux/bin64/../../src/phobos/std/algorithm/sorting.d(1027,1): std.algorithm.sorting.sort(alias less = "a < b", SwapStrategy ss = SwapStrategy.unstable, Range)(Range r) if ((ss == SwapStrategy.unstable && (hasSwappableElements!Range || hasAssignableElements!Range) || ss != SwapStrategy.unstable && hasAssignableElements!Range) && isRandomAccessRange!Range && hasSlicing!Range && hasLength!Range) source/services.d(168,5): Error: var has no effect in expression (theRange) dmd failed with exit code 1. And I have no clue what it is complaining about. I'd really appreciate any hint/help on this. Thanks, -- Bahman
Jul 25 2016
On 07/26/2016 09:35 AM, Bahman Movaqar wrote:I have a range which is the result of a couple of chained range operations, and each element is: Tuple!(string, "product", double, "price") Now I'd like to sort the range by "price" using: sort!((pp1, pp2) => cmp(pp1.price, pp2.price) > 0)(theRange) But I get a compile time error: source/services.d(166,63): Error: template std.algorithm.sorting.sort cannot deduce function from argument types !((pp1, pp2) => cmp(pp1.price, pp2.price) > 0)(MapResult!(__lambda5, Result)), candidates are: /home/bahman/Programs/D/dmd-2.071.0/linux/bin64/../../src/phobos/std/algorithm/sorting.d(1027,1): std.algorithm.sorting.sort(alias less = "a < b", SwapStrategy ss = SwapStrategy.unstable, Range)(Range r) if ((ss == SwapStrategy.unstable && (hasSwappableElements!Range || hasAssignableElements!Range) || ss != SwapStrategy.unstable && hasAssignableElements!Range) && isRandomAccessRange!Range && hasSlicing!Range && hasLength!Range) source/services.d(168,5): Error: var has no effect in expression (theRange) dmd failed with exit code 1.Alright...further experiments. The following works: sort!((pp1, pp2) => cmp(pp1.price, pp2.price) > 0)(theRange) So it may be something about what kind of range I'm passing to `sort`. Am I right? -- Bahman
Jul 25 2016
On 07/26/2016 10:11 AM, Bahman Movaqar wrote:Alright...further experiments. The following works: sort!((pp1, pp2) => cmp(pp1.price, pp2.price) > 0)(theRange) So it may be something about what kind of range I'm passing to `sort`. Am I right?I meant sort!((pp1, pp2) => cmp(pp1.price, pp2.price) > 0)(theRange.array) -- Bahman
Jul 25 2016
On Tuesday, July 26, 2016 10:11:43 Bahman Movaqar via Digitalmars-d-learn wrote:On 07/26/2016 09:35 AM, Bahman Movaqar wrote:/home/bahman/Programs/D/dmd-2.071.0/linux/bin64/../../src/phobos/std/algorithm/sorting.d(1027,1):I have a range which is the result of a couple of chained range operations, and each element is: Tuple!(string, "product", double, "price") Now I'd like to sort the range by "price" using: sort!((pp1, pp2) => cmp(pp1.price, pp2.price) > 0)(theRange) But I get a compile time error: source/services.d(166,63): Error: template std.algorithm.sorting.sort cannot deduce function from argument types !((pp1, pp2) => cmp(pp1.price, pp2.price) > 0)(MapResult!(__lambda5, Result)), candidates are:sort requires a random access range. Without knowing exactly which algorithms your using, I can't say for sure that that's the problem, but usually it is. Most of the time, you don't end up with a random access range after chaining several range-based functions. You _can_, but it depends entirely on which functions they are and the type of your original range. It's frequently the case that if you want to sort a range, you have to call array() on it to convert it to an array, and then you can sort the array. - Jonathan M Davisstd.algorithm.sorting.sort(alias less = "a < b", SwapStrategy ss = SwapStrategy.unstable, Range)(Range r) if ((ss == SwapStrategy.unstable && (hasSwappableElements!Range || hasAssignableElements!Range) || ss != SwapStrategy.unstable && hasAssignableElements!Range) && isRandomAccessRange!Range && hasSlicing!Range && hasLength!Range) source/services.d(168,5): Error: var has no effect in expression (theRange) dmd failed with exit code 1.Alright...further experiments. The following works: sort!((pp1, pp2) => cmp(pp1.price, pp2.price) > 0)(theRange) So it may be something about what kind of range I'm passing to `sort`. Am I right?
Jul 25 2016
26.07.2016 09:11, Jonathan M Davis via Digitalmars-d-learn пишет:It's frequently the case that if you want to sort a range, you have to call array() on it to convert it to an array, and then you can sort the array. - Jonathan M DavisAnother option is `makeIndex` (std.algorithm.sorting) and then sorting of that index.
Jul 26 2016
On 07/26/2016 11:42 AM, drug wrote:Another option is `makeIndex` (std.algorithm.sorting) and then sorting of that index.That's an interesting option; at least I don't have to touch the range. Thanks. -- Bahman
Jul 26 2016
On 07/26/2016 10:41 AM, Jonathan M Davis via Digitalmars-d-learn wrote:Thanks...that explains it. -- BahmanSo it may be something about what kind of range I'm passing to `sort`. Am I right?sort requires a random access range. Without knowing exactly which algorithms your using, I can't say for sure that that's the problem, but usually it is. Most of the time, you don't end up with a random access range after chaining several range-based functions. You _can_, but it depends entirely on which functions they are and the type of your original range. It's frequently the case that if you want to sort a range, you have to call array() on it to convert it to an array, and then you can sort the array.
Jul 26 2016