Input:
[{"bind":"0.0.0.0","port":"139","process":"smbd"},{"bind":"0.0.0.0","port":"8112","process":"deluge-web"},{"bind":"0.0.0.0","port":"22","process":"sshd"},{"bind":"0.0.0.0","port":"631","process":"cupsd"},{"bind":"0.0.0.0","port":"445","process":"smbd"},{"bind":"0.0.0.0","port":"10051","process":"zabbix_proxy"}]
JavaScript to sort from smallest port to biggest:
var obj = JSON.parse(value);
// define an empty row
var bigger = {};
// measure lenght of array
var countOfElements = obj.length;
// do a bubble sort algorithm
for (i = 0; i < countOfElements-1; i++) for (j = i+1; j < countOfElements; j++) {
// if value at the left side is bigger than value at right side
if ( parseInt(obj[i].port) > parseInt(obj[j].port) ) {
// then swap values
bigger = obj[j]; obj[j] = obj[i]; obj[i] = bigger;
}
}
return JSON.stringify(obj);
Outcome:
[{"bind":"0.0.0.0","port":"22","process":"sshd"},{"bind":"0.0.0.0","port":"139","process":"smbd"},{"bind":"0.0.0.0","port":"445","process":"smbd"},{"bind":"0.0.0.0","port":"631","process":"cupsd"},{"bind":"0.0.0.0","port":"8112","process":"deluge-web"},{"bind":"0.0.0.0","port":"10051","process":"zabbix_proxy"}]
No comments:
Post a Comment