Cody

# Problem 689. Sort an array of structures

Solution 1124997

Submitted on 17 Feb 2017 by Jihye Sofia Seo
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
a.n=1; a.name='a'; b.n=3; b.name='b'; c.n=2; c.name='c'; array = [a b c] % solution based on the first numeric field : array_sorted = [a c b] assert(isequal(your_fcn_name(array),array_sorted))

array = 1×3 struct array with fields: n name array_sorted = 1×3 struct array with fields: n name Afields = 2×1 cell array 'n' 'name' 2×1×3 cell array Acell(:,:,1) =  'a' Acell(:,:,2) =  'b' Acell(:,:,3) =  'c' sz = 2 1 3 Acell = 2×3 cell array    'a' 'b' 'c' Acell = 3×2 cell array  'a'  'c'  'b' 2×1×3 cell array Acell(:,:,1) =  'a' Acell(:,:,2) =  'c' Acell(:,:,3) =  'b' Asorted = 1×3 struct array with fields: n name

2   Pass
s1.age = 33 s1.name = 'aurelien' s2.age = 25 s2.name = 'jeanpierre' array = [s1 s2 ] % solution based on the first numeric field : array_sorted = [s2 s1] assert(isequal(your_fcn_name(array),array_sorted))

s1 = struct with fields: age: 33 s1 = struct with fields: age: 33 name: 'aurelien' s2 = struct with fields: age: 25 s2 = struct with fields: age: 25 name: 'jeanpierre' array = 1×2 struct array with fields: age name array_sorted = 1×2 struct array with fields: age name Afields = 2×1 cell array 'age' 'name' 2×1×2 cell array Acell(:,:,1) = [ 33] 'aurelien' Acell(:,:,2) = [ 25] 'jeanpierre' sz = 2 1 2 Acell = 2×2 cell array [ 33] [ 25] 'aurelien' 'jeanpierre' Acell = 2×2 cell array  'jeanpierre'  'aurelien' 2×1×2 cell array Acell(:,:,1) = [ 25] 'jeanpierre' Acell(:,:,2) = [ 33] 'aurelien' Asorted = 1×2 struct array with fields: age name