qsort

Version 1.0.0 (2.75 KB) by Bruno Luong
Sort data with user-supplied comparison function
6 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 (2026). qsort (https://uk.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
Version Published Release Notes
1.0.0