Python Forum
has anyone implemented a sort command in Python?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
has anyone implemented a sort command in Python?
#1
has anyone implemented a sort command in Python, even if it supports fewer features than the coreutils sort command?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
Yes. I worked through Introduction to Algorithms (Cormen and others), and did all the code in Python.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(Dec-16-2018, 04:39 AM)ichabod801 Wrote: Yes. I worked through Introduction to Algorithms (Cormen and others), and did all the code in Python.
are you giving it away, anywhere?

i am looking to modify a sort command to sort by keys of IP addresses.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
Example:

ip_list = ["192.168.2.108", "192.168.2.101", "192.168.2.155", "192.168.2.134"]

items = sorted(ip_list)
print(items)
print("\n".join(items))
Output:
['192.168.2.101', '192.168.2.108', '192.168.2.134', '192.168.2.155'] 192.168.2.101 192.168.2.108 192.168.2.134 192.168.2.155
Reply
#5
(Dec-16-2018, 05:14 AM)Skaperen Wrote: are you giving it away, anywhere?

i am looking to modify a sort command to sort by keys of IP addresses.

That was years ago, I'd could maybe find it, but it would be a pain. But I don't think it would be a good idea. Python's sort uses Timsort, which is a more efficient combination of merge and insertion sort. But Timsort is not covered in Introduction to Algorithms, so anything I did would be less efficient regardless of implementation issues. But there are implementation issues, as my sorts were pure Python, and Python's sort is implemented in C, a much faster language.

I would expect that Python's sort with a proper key function would be faster than anything I had implemented.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
>>> ip_list = ['192.168.2.168', '192.168.2.14', '192.168.2.105']
>>> sorted(ip_list)
['192.168.2.105', '192.168.2.14', '192.168.2.168']
>>> sorted(ip_list, key = lambda s: [int(x) for x in s.split('.')])
['192.168.2.14', '192.168.2.105', '192.168.2.168']
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
I see an issue here.
>>> ips = ['192.168.20.1', '192.168.125.43', '192.168.12.8']
>>> sorted(ips)
['192.168.12.8', '192.168.125.43', '192.168.20.1']
>>> 
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#8
Yes, that's what my code was intended to illustrate and fix.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#9
that was my point, sorts for text are not going to sort IP addresses the way we expect, though i have seen so many cases of IP addresses sorted that way. i wrote such a sort in C ages ago, but it needs work like adding support for key positions other than column 1. i was thinking it might be easier to start over and do it in python. if you have IP addresses in a numeric form then you could somehow use sorted() to do it.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#10
That's exactly what my code does (line 4): it converts the ip address to numeric, sorts based on that, and returns the strings.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how operators are implemented as function calls Skaperen 5 3,492 Dec-15-2018, 03:14 AM
Last Post: ichabod801
  Ugh, Someone Went and Implemented Case ichabod801 2 2,742 Sep-15-2017, 09:46 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

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