Add numeric sorting
This commit is contained in:
parent
57dcd961d5
commit
71aa4033a4
@ -26,7 +26,7 @@ const SeqUtils = {
|
|||||||
* @constant
|
* @constant
|
||||||
* @default
|
* @default
|
||||||
*/
|
*/
|
||||||
SORT_ORDER: ["Alphabetical (case sensitive)", "Alphabetical (case insensitive)", "IP address"],
|
SORT_ORDER: ["Alphabetical (case sensitive)", "Alphabetical (case insensitive)", "IP address", "Numeric"],
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sort operation.
|
* Sort operation.
|
||||||
@ -47,6 +47,8 @@ const SeqUtils = {
|
|||||||
sorted = sorted.sort(SeqUtils._caseInsensitiveSort);
|
sorted = sorted.sort(SeqUtils._caseInsensitiveSort);
|
||||||
} else if (order === "IP address") {
|
} else if (order === "IP address") {
|
||||||
sorted = sorted.sort(SeqUtils._ipSort);
|
sorted = sorted.sort(SeqUtils._ipSort);
|
||||||
|
} else if (order === "Numeric") {
|
||||||
|
sorted = sorted.sort(SeqUtils._numericSort);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (sortReverse) sorted.reverse();
|
if (sortReverse) sorted.reverse();
|
||||||
@ -221,6 +223,33 @@ const SeqUtils = {
|
|||||||
return a_ - b_;
|
return a_ - b_;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Comparison operation for sorting of numeric values.
|
||||||
|
*
|
||||||
|
* @private
|
||||||
|
* @param {string} a
|
||||||
|
* @param {string} b
|
||||||
|
* @returns {number}
|
||||||
|
*/
|
||||||
|
_numericSort: function _numericSort(a, b) {
|
||||||
|
let a_ = a.split(/([^\d]+)/),
|
||||||
|
b_ = b.split(/([^\d]+)/);
|
||||||
|
|
||||||
|
for (let i=0; i<a_.length && i<b.length; ++i) {
|
||||||
|
if (isNaN(a_[i]) && !isNaN(b_[i])) return 1; // Numbers after non-numbers
|
||||||
|
if (!isNaN(a_[i]) && isNaN(b_[i])) return -1;
|
||||||
|
if (isNaN(a_[i]) && isNaN(b_[i])) {
|
||||||
|
let ret = a_[i].localeCompare(b_[i]); // Compare strings
|
||||||
|
if (ret !== 0) return ret;
|
||||||
|
}
|
||||||
|
if (!isNaN(a_[i]) && !isNaN(a_[i])) { // Compare numbers
|
||||||
|
if (a_[i] - b_[i] !== 0) return a_[i] - b_[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
},
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export default SeqUtils;
|
export default SeqUtils;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user