Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Sort a Phone List by Last Name.  (Read 7291 times)

0 Members and 1 Guest are viewing this topic.

Salmon Trout

  • Guest
Re: Sort a Phone List by Last Name.
« Reply #15 on: January 08, 2018, 12:34:00 PM »
Can you do a quick test with the Powershell code I posted.  I think it will be around the same time as Dave's Jsort.

88,800 names
input:  sorted Z-A
output: sorted A-Z
GNU sort       0.40 sec
Benham jsort   6.13 sec
Powershell     8.49 sec
Batch method 664.99 sec (11 min 4.99 sec)




Sidewinder



    Guru

    Thanked: 139
  • Experience: Familiar
  • OS: Windows 10
Re: Sort a Phone List by Last Name.
« Reply #16 on: January 08, 2018, 02:18:17 PM »
Technically this is a Powershell one-iner, but I broke it down to 4 physical lines for readability. If you type this at the Powershell command prompt, just keep typing when the line wraps.

Code: [Select]
(Get-Content .\phone.txt) -replace '(.*?\d{3})\s(.*?)', '$1-$2' |
  ConvertFrom-Csv -Delimiter ' ' -Header First,Last,Phone |
  Sort-Object Last |
  Format-Table * -AutoSize

You can change the path and file name in the first line.

 8)
The true sign of intelligence is not knowledge but imagination.

-- Albert Einstein

Sidewinder



    Guru

    Thanked: 139
  • Experience: Familiar
  • OS: Windows 10
Re: Sort a Phone List by Last Name.
« Reply #17 on: January 08, 2018, 02:23:18 PM »
Technically this is a Powershell one-liner. I broke it into 4 physical lines for readability. If you do type this in a Powershell window, type all 4 lines as a single line and just keep typing when the line wraps. The interpreter will understand.

Code: [Select]
(Get-Content .\phone.txt) -replace '(.*?\d{3})\s(.*?)', '$1-$2' |
  ConvertFrom-Csv -Delimiter ' ' -Header First,Last,Phone |
  Sort-Object Last |
  Format-Table * -AutoSize

The path and file name can be changed as needed.

 8)
The true sign of intelligence is not knowledge but imagination.

-- Albert Einstein

Salmon Trout

  • Guest
Re: Sort a Phone List by Last Name.
« Reply #18 on: January 08, 2018, 03:33:54 PM »
It did it, but it echoed the sorted output to the console.


Sidewinder



    Guru

    Thanked: 139
  • Experience: Familiar
  • OS: Windows 10
Re: Sort a Phone List by Last Name.
« Reply #19 on: January 09, 2018, 06:07:18 AM »
Powershell has cmdlets for outputting to a file, however in this case redirection might be the simpler way to go.

Code: [Select]
(Get-Content .\Phone.txt) -replace '(.*?\d{3})\s(.*?)', '$1-$2' |
  ConvertFrom-Csv -Delimiter ' ' -Header First,Last,Phone |
  Sort-Object Last |
  Format-Table * -AutoSize -HideTableHeaders > .\Phone.new

If the preference is to have the headers in the output file, remove the -HideTableHeaders parameter from the Format-Table cmdlet.

 8)
The true sign of intelligence is not knowledge but imagination.

-- Albert Einstein

Salmon Trout

  • Guest
Re: Sort a Phone List by Last Name.
« Reply #20 on: January 09, 2018, 12:44:21 PM »
Added a timer:

Code: [Select]
$t = Measure-Command {
(Get-Content .\Notabs_names-rev-sorted.names.txt) -replace '(.*?\d{3})\s(.*?)', '$1-$2' |
  ConvertFrom-Csv -Delimiter ' ' -Header First,Last,Phone |
  Sort-Object Last |
  Format-Table * -AutoSize > out.txt
  }
echo "Time: $t"

Result:

Code: [Select]
Time: 00:00:13.2834694
The script is clearly doing more work than just sorting: for example it is justifying the columns (input file: 2.6 MB output file: 7.8 MB)


Salmon Trout

  • Guest
Re: Sort a Phone List by Last Name.
« Reply #21 on: January 10, 2018, 02:28:36 PM »
Python

88,800 names
0.138 seconds!

Code: [Select]
python sortfile.py > sorted.txt
2018-01-10 21:24:18.494000
2018-01-10 21:24:18.632000

Code: [Select]
from __future__ import print_function
from datetime import datetime
import csv
import operator

tstart = datetime.now()
reader = csv.reader(open("Notabs_names-rev-sorted.names.txt"), delimiter=" ")

for line in sorted(reader, key=operator.itemgetter(1)):
     print(" " . join(line))

tend = datetime.now()
print (tstart)
print (tend)

Salmon Trout

  • Guest
Re: Sort a Phone List by Last Name.
« Reply #22 on: January 11, 2018, 10:18:17 AM »
Better Python (27)

Code: [Select]
from __future__ import print_function
from datetime import datetime
import csv
import operator
tstart = datetime.now()
f = open('output.txt', 'w')
reader = csv.reader(open("input.txt"), delimiter=" ")
for line in sorted(reader, key=operator.itemgetter(1)):
    print(" " . join(line), file=f)
f.close()
tend = datetime.now()
print ("Elapsed", tend - tstart, "seconds")

88,800 names, 5 runs:

Code: [Select]
Elapsed 0:00:00.172000 seconds
Elapsed 0:00:00.156000 seconds
Elapsed 0:00:00.172000 seconds
Elapsed 0:00:00.157000 seconds
Elapsed 0:00:00.172000 seconds


Salmon Trout

  • Guest
Re: Sort a Phone List by Last Name.
« Reply #23 on: January 11, 2018, 10:54:50 AM »
Here are 88,800 names sorted randomly:



[attachment deleted by admin to conserve space]
« Last Edit: January 11, 2018, 11:14:31 AM by Salmon Trout »

Salmon Trout

  • Guest
Re: Sort a Phone List by Last Name.
« Reply #24 on: January 11, 2018, 11:13:20 AM »
Here's the other, 88,800 names sorted alphabetically by column (2) in reverse order. I notice that the sorted file compresses better.




[attachment deleted by admin to conserve space]

Salmon Trout

  • Guest
Re: Sort a Phone List by Last Name.
« Reply #25 on: January 11, 2018, 12:00:36 PM »
It's quicker to sort the reverse-sorted file than the randomly sorted file.

Code: [Select]
reverse 0:00:00.156000 seconds
random  0:00:00.265000 seconds