Cody

# Problem 293. Generalized sorting

Solution 1186148

Submitted on 11 May 2017 by T. Dunker
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

### Test Suite

Test Status Code Input and Output
1   Pass
[b2, ix2] = sortg([], @lt); assert(isempty(b2) && isempty(ix2));

2   Pass
a = rand(1, 100); [b1, ix1] = sort(a); [b2, ix2] = sortg(a, @lt); assert(isequal(b1, b2) && isequal(ix1, ix2));

3   Pass
a = rand(1, 100); [b1, ix1] = sort(a, 'descend'); [b2, ix2] = sortg(a, @gt); assert(isequal(b1, b2) && isequal(ix1, ix2));

4   Pass
a = randn(1, 100) + randn(1, 100)*1i; [~, ix1] = sort(abs(a)); b1 = a(ix1); [b2, ix2] = sortg(a, @(a, b) abs(a) < abs(b)); assert(isequal(b1, b2) && isequal(ix1, ix2));

5   Pass
a = randi(10, 1, 100); [b1, ix1] = sort(a); % MATLAB's sort() is stable [b2, ix2] = sortg(a, @lt); assert(isequal(b1, b2) && isequal(ix1, ix2));

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!