{"id":5655,"date":"2014-12-05T09:00:53","date_gmt":"2014-12-05T14:00:53","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=5655"},"modified":"2014-12-05T17:08:12","modified_gmt":"2014-12-05T22:08:12","slug":"natural-order-sorting","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2014\/12\/05\/natural-order-sorting\/","title":{"rendered":"Natural Order Sorting"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/answers\/contributors\/3208495\">Sean<\/a>'s pick this week is the suite of natural-order sorting tools by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/186085\">Stephen Cobeldick<\/a><\/p>\r\n      <div>\r\n         <ul>\r\n            <li><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/47433-natural-order-row-sort\">Natural-Order Row Sort<\/a><\/li>\r\n            <li><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/47434-natural-order-filename-sort\">Natural-Order Filename Sort<\/a><\/li>\r\n            <li><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/34464-customizable-natural-order-sort\">Customizable Natural-Order Sort<\/a><\/li>\r\n         <\/ul>\r\n      <\/div>\r\n   <\/introduction>\r\n   <p>If you work with data, there are many different naming schemes for files that you will likely encounter.  For some of these\r\n      the numerical sorting order might be the same as the ASCII order sorting order.  But it is not always, and how to deal with\r\n      this is a common question on <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/answers\/\">MATLAB Answers<\/a>.\r\n   <\/p>\r\n   <p>I typically try to set it up the following way when naming my own files using the \"%0xi\" format in <a href=\"\"><tt>num2str<\/tt><\/a>.  For example:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #0000FF\">for<\/span> ii = [1 2 17 495 3920]\r\n    <span style=\"color: #228B22\">% %04i - up to four zeros in front of the number.<\/span>\r\n    filenameii = [<span style=\"color: #A020F0\">'file'<\/span> num2str(ii,<span style=\"color: #A020F0\">'%04i'<\/span>) <span style=\"color: #A020F0\">'.csv'<\/span>];\r\n    disp(filenameii)\r\n<span style=\"color: #0000FF\">end<\/span><\/pre><pre style=\"font-style:oblique\">file0001.csv\r\nfile0002.csv\r\nfile0017.csv\r\nfile0495.csv\r\nfile3920.csv\r\n<\/pre><p>Stephen's natural-order sorting tools help sort files or names that do not necessarily have this setup.  For example, let's\r\n      look at the ASCII order of a few files using <a href=\"\"><tt>sortrows<\/tt><\/a>.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">files = {<span style=\"color: #A020F0\">'file1.csv'<\/span>,<span style=\"color: #A020F0\">'file111.csv'<\/span>,<span style=\"color: #A020F0\">'file21.csv'<\/span>,<span style=\"color: #A020F0\">'file211.csv'<\/span>}.';\r\ndisp(sortrows(files))<\/pre><pre style=\"font-style:oblique\">    'file1.csv'\r\n    'file111.csv'\r\n    'file21.csv'\r\n    'file211.csv'\r\n<\/pre><p>Numerically <i>file111.csv<\/i> should not be before <i>file21.csv<\/i>.  Now let's use <tt>natsort<\/tt> to do this for us:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">disp(natsort(files))<\/pre><pre style=\"font-style:oblique\">    'file1.csv'\r\n    'file21.csv'\r\n    'file111.csv'\r\n    'file211.csv'\r\n<\/pre><p>The other utilies that Stephen has provided allow more control over this and for the extension of it to not just working on\r\n      single elements of a cell but on a whole full file path.  For example, how should I sort the following?\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">files = {<span style=\"color: #A020F0\">'C:\\Documents\\Exp1\\test1.csv'<\/span>,<span style=\"color: #A020F0\">'C:\\Documents\\Exp2\\test1.csv'<\/span>,<span style=\"color: #A020F0\">'C:\\Documents\\Exp2\\test2.csv'<\/span>,<span style=\"color: #A020F0\">'C:\\Documents\\Exp1\\test2.csv'<\/span>}.';\r\ndisp(files)<\/pre><pre style=\"font-style:oblique\">    'C:\\Documents\\Exp1\\test1.csv'\r\n    'C:\\Documents\\Exp2\\test1.csv'\r\n    'C:\\Documents\\Exp2\\test2.csv'\r\n    'C:\\Documents\\Exp1\\test2.csv'\r\n<\/pre><p>Sorted by experiment:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">disp(natsortfiles(files))<\/pre><pre style=\"font-style:oblique\">    'C:\\Documents\\Exp1\\test1.csv'\r\n    'C:\\Documents\\Exp1\\test2.csv'\r\n    'C:\\Documents\\Exp2\\test1.csv'\r\n    'C:\\Documents\\Exp2\\test2.csv'\r\n<\/pre><p>To sort by test, we can split the file path into pieces and then use <tt>natsortrows<\/tt> on the pieces:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #228B22\">% Split on file separators<\/span>\r\nfilepieces = regexp(files, [<span style=\"color: #A020F0\">''<\/span> filesep <span style=\"color: #A020F0\">''<\/span>], <span style=\"color: #A020F0\">'split'<\/span>);\r\nfilepieces = vertcat(filepieces{:});\r\ndisp(filepieces)<\/pre><pre style=\"font-style:oblique\">    'C:'    'Documents'    'Exp1'    'test1.csv'\r\n    'C:'    'Documents'    'Exp2'    'test1.csv'\r\n    'C:'    'Documents'    'Exp2'    'test2.csv'\r\n    'C:'    'Documents'    'Exp1'    'test2.csv'\r\n<\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #228B22\">% Sort them by fourth column (test) then third column (experiment)<\/span>\r\n[~, idx] = natsortrows(filepieces,[4 3]);\r\ndisp(files(idx))<\/pre><pre style=\"font-style:oblique\">    'C:\\Documents\\Exp1\\test1.csv'\r\n    'C:\\Documents\\Exp2\\test1.csv'\r\n    'C:\\Documents\\Exp1\\test2.csv'\r\n    'C:\\Documents\\Exp2\\test2.csv'\r\n<\/pre><p>These files provide excellent help and are well documented.<\/p>\r\n   <p>My only suggestion for Stephen would be to provide these files together in one File Exchange entry (or as a fourth entry).\r\n       This is solely for the reason that I am lazy and downloading all of the separate zip files and unpacking them took an extra\r\n      few minutes.  However, since the second two files depend on <tt>natsort<\/tt>, they wouldn't work on their own without this process.\r\n   <\/p>\r\n   <h3>Comments<a name=\"9\"><\/a><\/h3>\r\n   <p>If given the choice, how do you choose to store your files?  What challenges do you face when you receive files from others\r\n      or from hardware?  Let us know <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=5655#respond\">below<\/a>.\r\n   <\/p>\r\n   <p>Give it a try and let us know what you think <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=5655#respond\">here<\/a> or leave a <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/34464-customizable-natural-order-sort#comments\">comment<\/a> for Stephen.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_ec46e475441e45d6a2f46a73cb1526cb() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='ec46e475441e45d6a2f46a73cb1526cb ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' ec46e475441e45d6a2f46a73cb1526cb';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        author = 'Sean de Wolski';\r\n        copyright = 'Copyright 2014 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }   \r\n      \r\n-->\r\n<\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_ec46e475441e45d6a2f46a73cb1526cb()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code \r\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2014b<br><\/p>\r\n<\/div>\r\n<!--\r\nec46e475441e45d6a2f46a73cb1526cb ##### SOURCE BEGIN #####\r\n%% Natural-Order Sorting\r\n%\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/answers\/contributors\/3208495\r\n% Sean>'s pick this week is the suite of natural-order sorting tools by\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/186085 Stephen Cobeldick>\r\n% \r\n% * <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/47433-natural-order-row-sort Natural-Order Row Sort>\r\n% * <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/47434-natural-order-filename-sort Natural-Order Filename Sort>\r\n% * <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/34464-customizable-natural-order-sort Customizable Natural-Order Sort>\r\n\r\n\r\n%% \r\n% If you work with data, there are many different naming schemes for files\r\n% that you will likely encounter.  For some of these the numerical sorting\r\n% order might be the same as the ASCII order sorting order.  But it is not\r\n% always, and how to deal with this is a common question on\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/answers\/ MATLAB Answers>.\r\n%\r\n% I typically try to set it up the following way when naming my own files\r\n% using the \"%0xi\" format in\r\n% <\r\n% |num2str|>.  For example:\r\n\r\nfor ii = [1 2 17 495 3920]\r\n    % %04i - up to four zeros in front of the number.\r\n    filenameii = ['file' num2str(ii,'%04i') '.csv'];\r\n    disp(filenameii)\r\nend\r\n\r\n\r\n%%\r\n% Stephen's natural-order sorting tools help sort files or names that do\r\n% not necessarily have this setup.  For example, lets look at the ASCII\r\n% order of a few files using\r\n% <\r\n% |sortrows|>.\r\n\r\nfiles = {'file1.csv','file111.csv','file21.csv','file211.csv'}.';\r\ndisp(sortrows(files))\r\n\r\n\r\n%%\r\n% Numerically _file111.csv_ should not be before _file21.csv_.  Now let's\r\n% use |natsort| to do this for us:\r\n\r\ndisp(natsort(files))\r\n\r\n\r\n%% \r\n% The other utilies that Stephen has provided allow more control over this\r\n% and for the extension of it to not just working on single elements of a\r\n% cell but on a whole full file path.  For example, how should I sort the\r\n% following?\r\n\r\nfiles = {'C:\\Documents\\Exp1\\test1.csv','C:\\Documents\\Exp2\\test1.csv','C:\\Documents\\Exp2\\test2.csv','C:\\Documents\\Exp1\\test2.csv'}.';\r\ndisp(files)\r\n\r\n%% \r\n% Sorted by experiment:\r\n\r\ndisp(natsortfiles(files))\r\n\r\n%%\r\n% To sort by test, we can split the file path into pieces and then use\r\n% |natsortrows| on the pieces:\r\n\r\n% Split on file separators\r\nfilepieces = regexp(files, ['' filesep ''], 'split');\r\nfilepieces = vertcat(filepieces{:});\r\ndisp(filepieces)\r\n\r\n%% \r\n\r\n% Sort them by fourth column (test) then third column (experiment)\r\n[~, idx] = natsortrows(filepieces,[4 3]);\r\ndisp(files(idx))\r\n\r\n\r\n%%\r\n% These files provide excellent help and are well documented.\r\n%\r\n% My only suggestion for Stephen would be to provide these files together\r\n% in one File Exchange entry (or as a fourth entry).  This is solely for\r\n% the reason that I am lazy and downloading all of the separate zip files\r\n% and unpacking them took an extra few minutes.  However, since the second\r\n% two files depend on |natsort|, they wouldn't work on their own without\r\n% this process.\r\n\r\n\r\n\r\n%% Comments\r\n% \r\n% If given the choice, how do you choose to store your files?  What\r\n% challenges do you face when you receive files from others or from\r\n% hardware?  Let us know <https:\/\/blogs.mathworks.com\/pick\/?p=5655#respond\r\n% below>.\r\n%\r\n% Give it a try and let us know what you think\r\n% <https:\/\/blogs.mathworks.com\/pick\/?p=5655#respond here> or leave a\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/34464-customizable-natural-order-sort#comments\r\n% comment> for Stephen.\r\n\r\n##### SOURCE END ##### ec46e475441e45d6a2f46a73cb1526cb\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      Sean's pick this week is the suite of natural-order sorting tools by Stephen Cobeldick\r\n      \r\n         \r\n           ... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2014\/12\/05\/natural-order-sorting\/\">read more >><\/a><\/p>","protected":false},"author":87,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/5655"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/87"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=5655"}],"version-history":[{"count":4,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/5655\/revisions"}],"predecessor-version":[{"id":5736,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/5655\/revisions\/5736"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=5655"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=5655"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=5655"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}