JustCurious
JustCurious

Reputation: 111

Require sorting on IP address using js

I am using https://www.kryogenix.org/code/browser/sorttable for sorting the columns in my table. This works great without using any plugin or so.

But one column is of IP addresses and this script is not sorting the column for IP address. eg. If only I could sort below for IP address.

10.132.14.12
10.132.14.11
10.132.14.14
10.132.14.9

Please note that the IP addresses generated over here are automatic. So I cannot know what range of IP address I will receive.

Upvotes: 2

Views: 8756

Answers (5)

Tesla
Tesla

Reputation: 189

The following code allows you any type of string by matching its ip address using RegExp.

const arrayLines = '100.98.242.115:DarkDAYSS16\n100.98.235.61:EnesAbii47\n100.98.236.188:Enesesme\n100.98.234.61:ParikmaxerKaya\n100.98.239.26:kino_moflos\nc\n1.1.1.1\n100.98.238.233:MaxFedoseev2007\n100.98.233.98:L20061212D\ndef\n100.98.236.145:enes125ht\n100.98.236.91:LOGAN_LOBEZNO888\n100.98.237.249:pequee192\nabc\n100.98.233.126:psh910\n100.98.238.56:LOSE-TODOO\n100.98.234.160:Marco_08capu\n100.98.239.182:Umut_Alinca\n100.98.241.52:kort196\n100.98.237.119:JoshAcco\n100.98.237.211:kevinowenss123\n100.98.240.64:Eren057_016\n100.98.240.117:aaronssalguero'.split('\n');
const regExp = /.*?(?<ip_address>(\b25[0-5]|\b2[0-4]\d|\b[01]?\d{1,2})(\.(25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})/;

const ip2long = (ip_address) => {
    let longValue = 0;
    for (const [index, part] of ip_address.split('.').entries()) longValue += part * [0x1_00_00_00, 0x1_00_00, 0x1_00, 1][index];
    return longValue;
}

// Response: (20) ['1.1.1.1', '100.98.233.98:L20061212D', '100.98.233.126:psh910', '100.98.234.61:ParikmaxerKaya', '100.98.234.160:Marco_08capu', '100.98.235.61:EnesAbii47', '100.98.236.91:LOGAN_LOBEZNO888', '100.98.236.145:enes125ht', '100.98.236.188:Enesesme', '100.98.237.119:JoshAcco', '100.98.237.211:kevinowenss123', '100.98.237.249:pequee192', '100.98.238.56:LOSE-TODOO', '100.98.238.233:MaxFedoseev2007', '100.98.239.26:kino_moflos', '100.98.239.182:Umut_Alinca', '100.98.240.64:Eren057_016', '100.98.240.117:aaronssalguero', '100.98.241.52:kort196', '100.98.242.115:DarkDAYSS16']
arrayLines.filter(string_ => string_.match(regExp) !== null).sort((a, b) => {
    let aMatchIPAddress = a.match(regExp),
        bMatchIPAddress = b.match(regExp);

    let aIP2long = ip2long(aMatchIPAddress.groups.ip_address),
        bIP2long = ip2long(bMatchIPAddress.groups.ip_address);

    return aIP2long - bIP2long;
});

Upvotes: 0

iPzard
iPzard

Reputation: 2368

DRY version of Vasanth's answer.

const makeIpNumber = (ip) => Number(
  ip.split('.')
    .map((subString) => (`00${subString}`).slice(-3))
    .join('')
);

arrayOfIps.sort((a, b) => makeIpNumber(a) - makeIpNumber(b));

Upvotes: 0

apla
apla

Reputation: 51

This function makes numeric sort of IPv4 addresses:

function compareIPAddresses(a, b) {
  const numA = Number(
    a.split('.')
      .map((num, idx) => num * Math.pow(2, (3 - idx) * 8))
      .reduce((a, v) => ((a += v), a), 0)
  );
  const numB = Number(
    b.split('.')
      .map((num, idx) => num * Math.pow(2, (3 - idx) * 8))
      .reduce((a, v) => ((a += v), a), 0)
  );
  return numA - numB;
}

const ipAddresses = [
  '10.132.14.12',
  '10.132.14.11',
  '10.132.14.14',
  '10.132.14.9',
];

const sorted = ipAddresses.sort(compareIPAddresses);

console.log(sorted);

Upvotes: 2

jagadish
jagadish

Reputation: 11

let sortedIpArr = ipArr.sort((a, b) =>{

  return a.split('.')[0] - b.split('.')[0] || a.split('.')[1] - b.split('.')[1] || a.split('.')[2] - b.split('.')[2] || a.split('.')[3] - b.split('.')[3]
});

console.log("sorted ip arr: ", sortedIpArr);

Upvotes: -1

Vasanth Sriram
Vasanth Sriram

Reputation: 1315

Just adding my solution here if anyone ends up here after search:

arrayOfIps
.sort((a, b) => {
        const num1 = Number(a.split(".").map((num) => (`000${num}`).slice(-3) ).join(""));
        const num2 = Number(b.split(".").map((num) => (`000${num}`).slice(-3) ).join(""));
        return num1-num2;
});

You could also use padStart instead of slice, if you are using newer versions of nodejs.

Upvotes: 28

Related Questions