qsort

Version 1.0.0 (2.75 KB) by Bruno Luong
Sort data with user-supplied comparison function
5 Downloads
Updated 31 Mar 2022

View License

Some examples of usage:
% Sort numerical array in descending order
A=rand(1,100000); tic; [As,is]=qsort(A, @(x,y) y-x); toc
% Sort strings
s = string(randi(200,1,20))
ss = qsort(s, @(x,y) double(x)-double(y))
% Sort structures
s = struct('name',{'smith' 'jackson' 'anna'},'yearofbirth', {40 70 60});
ss = qsort(s, @(x,y) x.yearofbirth-y.yearofbirth);
{ss.name} % returns {'smith'} {'anna'} {'jackson'}
[ss.yearofbirth] % returns 40 60 70
% Sort cell array
c = num2cell(struct('name',{'smith' 'jackson' 'anna'},'yearofbirth', {40 70 60}));
cs = qsort(c, @(x,y) x{1}.yearofbirth-y{1}.yearofbirth);
cs{:}

Cite As

Bruno Luong (2024). qsort (https://www.mathworks.com/matlabcentral/fileexchange/109104-qsort), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2022a
Compatible with any release
Platform Compatibility
Windows macOS Linux
Tags Add Tags

Community Treasure Hunt

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

Start Hunting!
Version Published Release Notes
1.0.0