summaryrefslogtreecommitdiff
path: root/lib/sortBy.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/sortBy.js')
-rw-r--r--lib/sortBy.js134
1 files changed, 118 insertions, 16 deletions
diff --git a/lib/sortBy.js b/lib/sortBy.js
index 4477677..7a1b9fb 100644
--- a/lib/sortBy.js
+++ b/lib/sortBy.js
@@ -24,31 +24,133 @@ import awaitify from './internal/awaitify'
* @returns {Promise} a promise, if no callback passed
* @example
*
- * async.sortBy(['file1','file2','file3'], function(file, callback) {
- * fs.stat(file, function(err, stats) {
- * callback(err, stats.mtime);
+ * // bigfile.txt is a file that is 251100 bytes in size
+ * // mediumfile.txt is a file that is 11000 bytes in size
+ * // smallfile.txt is a file that is 121 bytes in size
+ *
+ * // asynchronous function that returns the file size in bytes
+ * function getFileSizeInBytes(file, callback) {
+ * fs.stat(file, function(err, stat) {
+ * if (err) {
+ * return callback(err);
+ * }
+ * callback(null, stat.size);
* });
- * }, function(err, results) {
- * // results is now the original array of files sorted by
- * // modified date
- * });
+ * }
+ *
+ * // Using callbacks
+ * async.sortBy(['mediumfile.txt','smallfile.txt','bigfile.txt'], getFileSizeInBytes,
+ * function(err, results) {
+ * if (err) {
+ * console.log(err);
+ * } else {
+ * console.log(results);
+ * // results is now the original array of files sorted by
+ * // file size (ascending by default), e.g.
+ * // [ 'smallfile.txt', 'mediumfile.txt', 'bigfile.txt']
+ * }
+ * }
+ * );
*
* // By modifying the callback parameter the
* // sorting order can be influenced:
*
* // ascending order
- * async.sortBy([1,9,3,5], function(x, callback) {
- * callback(null, x);
- * }, function(err,result) {
- * // result callback
- * });
+ * async.sortBy(['mediumfile.txt','smallfile.txt','bigfile.txt'], function(file, callback) {
+ * getFileSizeInBytes(file, function(getFileSizeErr, fileSize) {
+ * if (getFileSizeErr) return callback(getFileSizeErr);
+ * callback(null, fileSize);
+ * });
+ * }, function(err, results) {
+ * if (err) {
+ * console.log(err);
+ * } else {
+ * console.log(results);
+ * // results is now the original array of files sorted by
+ * // file size (ascending by default), e.g.
+ * // [ 'smallfile.txt', 'mediumfile.txt', 'bigfile.txt']
+ * }
+ * }
+ * );
*
* // descending order
- * async.sortBy([1,9,3,5], function(x, callback) {
- * callback(null, x*-1); //<- x*-1 instead of x, turns the order around
- * }, function(err,result) {
- * // result callback
+ * async.sortBy(['bigfile.txt','mediumfile.txt','smallfile.txt'], function(file, callback) {
+ * getFileSizeInBytes(file, function(getFileSizeErr, fileSize) {
+ * if (getFileSizeErr) {
+ * return callback(getFileSizeErr);
+ * }
+ * callback(null, fileSize * -1);
+ * });
+ * }, function(err, results) {
+ * if (err) {
+ * console.log(err);
+ * } else {
+ * console.log(results);
+ * // results is now the original array of files sorted by
+ * // file size (ascending by default), e.g.
+ * // [ 'bigfile.txt', 'mediumfile.txt', 'smallfile.txt']
+ * }
+ * }
+ * );
+ *
+ * // Error handling
+ * async.sortBy(['mediumfile.txt','smallfile.txt','missingfile.txt'], getFileSizeInBytes,
+ * function(err, results) {
+ * if (err) {
+ * console.log(err);
+ * // [ Error: ENOENT: no such file or directory ]
+ * } else {
+ * console.log(results);
+ * }
+ * }
+ * );
+ *
+ * // Using Promises
+ * async.sortBy(['mediumfile.txt','smallfile.txt','bigfile.txt'], getFileSizeInBytes)
+ * .then( results => {
+ * console.log(results);
+ * // results is now the original array of files sorted by
+ * // file size (ascending by default), e.g.
+ * // [ 'smallfile.txt', 'mediumfile.txt', 'bigfile.txt']
+ * }).catch( err => {
+ * console.log(err);
* });
+ *
+ * // Error handling
+ * async.sortBy(['mediumfile.txt','smallfile.txt','missingfile.txt'], getFileSizeInBytes)
+ * .then( results => {
+ * console.log(results);
+ * }).catch( err => {
+ * console.log(err);
+ * // [ Error: ENOENT: no such file or directory ]
+ * });
+ *
+ * // Using async/await
+ * (async () => {
+ * try {
+ * let results = await async.sortBy(['bigfile.txt','mediumfile.txt','smallfile.txt'], getFileSizeInBytes);
+ * console.log(results);
+ * // results is now the original array of files sorted by
+ * // file size (ascending by default), e.g.
+ * // [ 'smallfile.txt', 'mediumfile.txt', 'bigfile.txt']
+ * }
+ * catch (err) {
+ * console.log(err);
+ * }
+ * })();
+ *
+ * // Error handling
+ * async () => {
+ * try {
+ * let results = await async.sortBy(['missingfile.txt','mediumfile.txt','smallfile.txt'], getFileSizeInBytes);
+ * console.log(results);
+ * }
+ * catch (err) {
+ * console.log(err);
+ * // [ Error: ENOENT: no such file or directory ]
+ * }
+ * }
+ *
*/
function sortBy (coll, iteratee, callback) {
var _iteratee = wrapAsync(iteratee);