addressalign-toparrow-leftarrow-rightbackbellblockcalendarcameraccwcheckchevron-downchevron-leftchevron-rightchevron-small-downchevron-small-leftchevron-small-rightchevron-small-upchevron-upcircle-with-checkcircle-with-crosscircle-with-pluscrossdots-three-verticaleditemptyheartexporteye-with-lineeyefacebookfolderfullheartglobegmailgooglegroupshelp-with-circleimageimagesinstagramFill 1linklocation-pinm-swarmSearchmailmessagesminusmoremuplabelShape 3 + Rectangle 1ShapeoutlookpersonJoin Group on CardStartprice-ribbonShapeShapeShapeShapeImported LayersImported LayersImported Layersshieldstartickettrashtriangle-downtriangle-uptwitteruserwarningyahoo

Re: [GHGLUG] A brain freeze question on sort?

From: Dan B.
Sent on: Tuesday, July 24, 2012 8:31 AM
This is not the answer you were looking for, but since no one else
answered yet...

Here's a ruby script that'll achieve what you're after. Save this in a
file called (say) sorter.rb:

    lines = File.read(ARGV[0]).s­plit("\n").map(&­:chomp)
    puts lines.sort_by { |line|
      alpha, num = line.split(' ')
      [alpha, num.to_i]
    }

To run it:

$ ruby sorter.rb your_data.txt

It'll print the sorted data to stdout.

HTH!
Dan

On Mon, Jul 23, 2012 at 9:00 PM, BobMCT <[address removed]> wrote:
> Hello Luggers,
>
> I hope someone is reading these.
>
> I am trying to sort a simple file into order of alphamerically by the first
> field and numerically by the second field witin.  The fields are separated
> by a space.
>
> Here is a snippet of my data:
>
> $_address1 1006
> $_address1 1079
> $_address1 1080
> $_address1 1214
> $_address1 1228
> $_address1 45
> $_address1 716
> $_address1 747
> $_address1 748
> $_address1 842
> $_address2 1007
> $_address2 1085
> $_address2 1214
> $_address2 1229
> $_address2 46
> $_address2 717
> $_address2 753
> $_address2 842
>
> And as you can see the 2nd field appears to be random within the first.
> I've tried numerous tricks with sort such as:
>
> -g to get numeric sorting (but that didn't work because it expects the
> number to be in characters 1+
>
> -n to force a numeric sort but again that worked like the -g
>
> -k 1.1,2.1  to define the 1st field as the first key starting in pos 1 and
> the second field as the 2nd key starting in its relative position 1.  Both
> didn't work and gave me much the same output as above.  Then I added the
> letter n to the end of the 2nd field "-k 1.1,2.1n" and this didn't work at
> all.
>
> I've ready tons of material on this today and tried virtually everything I
> found.  Can any of you luggers come up with some sound ideas?
>
> Thanks all.  Bob
>
>
>
> --
> Please Note: If you hit "REPLY", your message will be sent to everyone on
> this mailing list ([address removed])
> http://www.meetup...­
> This message was sent by BobMCT ([address removed]) from Greater Hartford
> GNU Linux Users Group.
> To learn more about BobMCT, visit his/her member profile:
> http://www.meetup...­
> Set my mailing list to email me
>
> As they are sent
> http://www.meetup...­
>
> In one daily email
> http://www.meetup...­
>
> Don't send me mailing list messages
> http://www.meetup...­
> Meetup, PO Box 4668 #37895 New York, New York[masked] |
> [address removed]
>



-- 
twitter @danbernier
http://wordcram.o...­ | http://newhavenrb...­

People in this
Meetup are also in:

Sign up

Meetup members, Log in

By clicking "Sign up" or "Sign up using Facebook", you confirm that you accept our Terms of Service & Privacy Policy