Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sort sets by item values
#3
You want to sort by: (port, right_ip_address, left_ip_address)

Here is a key function to sort them.

import ipaddress


def ip_sorter(text):
    parts = text.split()
    port = int(parts[-1])
    addr1 = int(ipaddress.ip_address(parts[-3]))
    addr2 = int(ipaddress.ip_address(parts[3]))
    return port, addr1, addr2
Then call:
sorted(your_set, key=ip_sorter)
For each element in your_set, the key function is called with the element as argument.
The sorter grabs port, addr1 and addr2. addr1/2 are converted to an ip_address object, then it's converted to an integer.
The port must be also an integer.

finally the sorter returns a tuple with 3 integers. The sorted function uses this information to get the items in the right order.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
Sort sets by item values - by Sergey - Apr-18-2019, 11:33 AM
RE: Sort sets by item values - by Yoriz - Apr-18-2019, 11:59 AM
RE: Sort sets by item values - by DeaD_EyE - Apr-18-2019, 12:00 PM
RE: Sort sets by item values - by Sergey - Apr-18-2019, 12:42 PM
RE: Sort sets by item values - by Sergey - Apr-19-2019, 10:50 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to sort .csv file test log which item first fail and paint color SamLiu 24 5,726 Sep-03-2022, 07:32 AM
Last Post: Pedroski55
Photo a.sort() == b.sort() all the time 3lnyn0 1 1,452 Apr-19-2022, 06:50 PM
Last Post: Gribouillis
  Peculiar pattern from printing of sets SahandJ 7 1,883 Dec-29-2021, 06:31 PM
Last Post: bowlofred
  Remove an item from a list contained in another item in python CompleteNewb 19 6,245 Nov-11-2021, 06:43 AM
Last Post: Gribouillis
  How does one combine 2 data sets ? detlefschmitt 2 1,800 Sep-03-2021, 03:38 AM
Last Post: detlefschmitt
  How to sort values descending from a row in a dataframe using python sankarachari 1 1,505 Aug-16-2021, 08:55 AM
Last Post: jamesaarr
  Looping Through Large Data Sets JoeDainton123 10 4,687 Oct-18-2020, 02:58 PM
Last Post: buran
  comprehension for sets Skaperen 2 1,981 Aug-07-2020, 10:12 PM
Last Post: Skaperen
  Sort y axis by descening values matplotlib mrsenorchuck 0 3,918 Dec-08-2019, 08:13 PM
Last Post: mrsenorchuck
  sort lists of lists with multiple criteria: similar values need to be treated equal stillsen 2 3,536 Mar-20-2019, 08:01 PM
Last Post: stillsen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020