How to sort ip address in ascending order

后端 未结 9 1821
自闭症患者
自闭症患者 2020-12-16 02:32

Is there any method to sort this? Or do I just need to split it and use a loop to compare? Input

123.4.245.23
104.244.253.29
1.198.3.93
32.183.93.40
104.30.2         


        
9条回答
  •  执念已碎
    2020-12-16 03:27

    How about this plain logic:

    Ip Address: [10.1.1.2, 10.22.33.11, 10.12.23.12]

    1) Fill in the IP to complete 12 digits format with prefix 0: like [010.001.001.002, 010.022.033.011, 010.012.023,012]

    2) Remove "."s to make it complete string of digits: [010001001002, 010022033011, 010012023012]

    3) Apply Sort [010001001002, 010012023012, 010022033011]

    4) Retain dots after every 3 digits: [010.001.001.002, 010.012.023.012, 010.022.033.011]

    5) Remove prefix 0's [10.1.1.2, 10.12.23.12, 10.22.33.11]

    6) Sorted!

提交回复
热议问题