mirror of
https://github.com/gchq/CyberChef
synced 2024-11-15 17:07:57 +00:00
Add numeric sorting
This commit is contained in:
parent
57dcd961d5
commit
71aa4033a4
1 changed files with 30 additions and 1 deletions
|
@ -26,7 +26,7 @@ const SeqUtils = {
|
|||
* @constant
|
||||
* @default
|
||||
*/
|
||||
SORT_ORDER: ["Alphabetical (case sensitive)", "Alphabetical (case insensitive)", "IP address"],
|
||||
SORT_ORDER: ["Alphabetical (case sensitive)", "Alphabetical (case insensitive)", "IP address", "Numeric"],
|
||||
|
||||
/**
|
||||
* Sort operation.
|
||||
|
@ -47,6 +47,8 @@ const SeqUtils = {
|
|||
sorted = sorted.sort(SeqUtils._caseInsensitiveSort);
|
||||
} else if (order === "IP address") {
|
||||
sorted = sorted.sort(SeqUtils._ipSort);
|
||||
} else if (order === "Numeric") {
|
||||
sorted = sorted.sort(SeqUtils._numericSort);
|
||||
}
|
||||
|
||||
if (sortReverse) sorted.reverse();
|
||||
|
@ -221,6 +223,33 @@ const SeqUtils = {
|
|||
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;
|
||||
|
|
Loading…
Reference in a new issue