Skip to content

Commit

Permalink
Merge pull request #33 from shujianbu/master
Browse files Browse the repository at this point in the history
change the sort to natural sorting
  • Loading branch information
shujianbu committed Sep 13, 2014
2 parents c860dfb + 0f37e5c commit 991a7fc
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 28 deletions.
41 changes: 41 additions & 0 deletions lib/naturalSort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Natural Sort algorithm for Javascript - Version 0.6 - Released under MIT license
* Author: Jim Palmer (based on chunking idea from Dave Koelle)
* Contributors: Mike Grier (mgrier.com), Clint Priest, Kyle Adams, guillermo
*/
function naturalSort (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/,
// convert all to strings and trim()
x = a.toString().replace(sre, '') || '',
y = b.toString().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;
// 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;
}
29 changes: 2 additions & 27 deletions src/taxonomyViz.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -427,31 +427,9 @@ class taxonomyViz
if selected_phinchID_array_clone.length > 0
for i in [0..selected_phinchID_array_clone.length-1]
phinchID_map.push({'index': i, 'phinchName': selected_phinchID_array_clone[i]})
if !numericFlag || !@IsNumeric(selected_phinchID_array_clone[i])
numericFlag = false

if numericFlag # 1 phinch names are numeric values
phinchID_map.sort (a,b) ->
if !sortDescFlag
return a.phinchName - b.phinchName
else
return b.phinchName - a.phinchName
else # 2 phinch names are strings
phinchID_map.sort (a,b) ->
nameA = String(a.phinchName).toLowerCase()
nameB = String(b.phinchName).toLowerCase()
if !sortDescFlag
if(nameA < nameB)
return -1
if(nameA > nameB)
return 1
return 0
else
if(nameA > nameB)
return -1
if(nameA < nameB)
return 1
return 0
phinchID_map.sort (a,b) ->
return naturalSort(a.phinchName, b.phinchName)

for i in [0..selected_phinchID_array_clone.length-1]
sorted_selected_phinchID_array[i] = phinchID_map[i].index
Expand Down Expand Up @@ -1848,7 +1826,4 @@ class taxonomyViz
re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
return re.test(email);

IsNumeric: (input) ->
return !isNaN(parseFloat(input)) && isFinite(input)

window.taxonomyViz = taxonomyViz
3 changes: 2 additions & 1 deletion viz.html
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,8 @@ <h1>NOT SUPPORTED</h1>
<script src="lib/d3.sankey.js"></script>
<script src="lib/lodash.min.js"></script>
<script src="lib/FileSaver.js"></script>

<script src="lib/naturalSort.js"></script>

<script src="scripts/taxonomyViz.js"></script>
<script src="scripts/init.js"></script>
<script> var app; window.onload = function() { app = new init('viz');} </script>
Expand Down

0 comments on commit 991a7fc

Please sign in to comment.