Skip to content
Snippets Groups Projects
Commit ad57beb0 authored by barillac's avatar barillac
Browse files

natural sort function added

SVN: 34103
parent b38c3fbe
No related branches found
No related tags found
No related merge requests found
......@@ -24,7 +24,66 @@
<link type="text/css" rel="stylesheet" href="./css/style.css" />
<script type="text/javascript" src="./js/search.js"></script>
<script type="text/javascript">
<script type="text/javascript">
/**
*
* Natural Sort algorithm for Javascript - Version 0.7 - Released under MIT license
* Author: Jim Palmer (based on chunking idea from Dave Koelle)
*
* @param a first string to compare
* @param b second string to compare
* @return 1 if a > b, 0 if a == b, -1 if a < b
*
* @see http://www.overset.com/2008/09/01/javascript-natural-sort-algorithm-with-unicode-support/
*/
var naturalSort = function(a, b) {
var re = /(^-?[0-9]+(\.?[0-9]*)[df]?e?[0-9]?$|^0x[0-9a-f]+$|[0-9]+)/gi,
sre = /(^[ ]*|[ ]*$)/g,
dre = /(^([\w ]+,?[\w ]+)?[\w ]+,?[\w ]+\d+:\d+(:\d+)?[\w]?|^\d{1,4}[\/\-]\d{1,4}[\/\-]\d{1,4}|^\w+, \w+ \d+, \d{4})/,
hre = /^0x[0-9a-f]+$/i,
ore = /^0/,
i = function(s) {
return naturalSort.insensitive && ('' + s).toLowerCase() || '' + s;
},
// convert all to strings strip whitespace
x = i(a).replace(sre, '') || '',
y = i(b).replace(sre, '') || '',
// chunk/tokenize
xN = x.replace(re, '\0$1\0').replace(/\0$/, '').replace(/^\0/,'').split('\0'),
yN = y.replace(re, '\0$1\0').replace(/\0$/, '').replace(/^\0/,'').split('\0'),
// numeric, hex or date detection
xD = parseInt(x.match(hre)) || (xN.length != 1 && x.match(dre) && Date.parse(x)),
yD = parseInt(y.match(hre)) || xD && y.match(dre) && Date.parse(y) || null, oFxNcL, oFyNcL;
// first try and sort Hex codes or Dates
if (yD)
if (xD < yD)
return -1;
else if (xD > yD)
return 1;
// natural sorting through split numeric strings and default strings
for (var cLoc = 0, numS = Math.max(xN.length, yN.length); cLoc < numS; cLoc++) {
// find floats not starting with '0', string or 0 if not defined (Clint Priest)
oFxNcL = !(xN[cLoc] || '').match(ore) && parseFloat(xN[cLoc]) || xN[cLoc] || 0;
oFyNcL = !(yN[cLoc] || '').match(ore) && parseFloat(yN[cLoc]) || yN[cLoc] || 0;
// handle numeric vs string comparison - number < string - (Kyle Adams)
if (isNaN(oFxNcL) !== isNaN(oFyNcL)) {
return (isNaN(oFxNcL)) ? 1 : -1;
}
// rely on string comparison if different types - i.e. '02' < 2 != '02' < '2'
else if (typeof oFxNcL !== typeof oFyNcL) {
oFxNcL += '';
oFyNcL += '';
}
if (oFxNcL < oFyNcL)
return -1;
if (oFxNcL > oFyNcL)
return 1;
}
return 0;
};
//
// Global variables
//
......@@ -196,21 +255,23 @@
$experimentLevel
.append($samplesMenu);
clusters.sort(function(clusterA, clusterB) {
return naturalSort(clusterA.properties['NAME'], clusterB.properties['NAME']);
});
for (var i = 0; i < clusters.length; i++) {
var explandFolderClass = "";
if(filterText) {
explandFolderClass = " expanded";
explandFolderClass = "expanded";
}
//ORIGINAL CODE
// var $sampleLevel = $("<li>", {'class' : 'folder' + explandFolderClass})
// .append("<a href='javascript:showSamples(\""+clusters[i].identifier+"\")'>" + "Cluster " + clusters[i].code.substring(clusters[i].code.indexOf("CLUSTER")+7) + "</a>");
var $sampleLevel = $("<li>", {'class' : 'folder' + explandFolderClass})
var $sampleLevel = $("<li>", {'class' : 'folder' + explandFolderClass})
.append("<a href='javascript:showSamples(\""+clusters[i].identifier+"\")'>" + clusters[i].properties['NAME'] + "</a>");
var $containedSamplesMenu = $("<ul>");
$sampleLevel
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment