File Exchange

image thumbnail

Generalized Sorting

version (2.1 KB) by Nathan Thern
Sort any kind of data based on user-defined criteria.

1 Download

Updated 21 Jan 2005

View License

QUICKSORT allows the user to sort by any desired criteria. The user passes the data and a comparison function to the QUICKSORT and QUICKSORT returns an indexing vector sorted based on the comparison function.

The comparison function may be an inline function or a function handle. Function handles are an order of magnitude faster than inline functions. I use the TMP_HANDLE utility for creating temporary function handles in MATLAB 6.

QUICKSORT is implemented in m-code and uses a modified quicksort algorithm which sorts by "less than", "equal to" and "greater than". It is much slower than the builtin sort function, but also much more flexible.

Cite As

Nathan Thern (2021). Generalized Sorting (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (7)


Jan Moringen

I used the quicksort function with a cellarray of objects and a comparison-function that compared a specific attribute of the objects. It worked nicely. I like how the source array is not touched but a list of indices is returned instead.

Joe Daz

This function uses two arguments instead of one (a 1xn vector). Complicated if you just want to sort a simple vector.

Guido Longoni

Thank you very much!

Miks Böllér

if it "doesnt work", maybe its just you who couldnt use it... have you read the comments in m file?

Urban Novak

Thans, it works perfectly.

Steve Block

doesn't work

MATLAB Release Compatibility
Created with R10
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!