CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
How to sort the following data

 



cliffyiu
Novice

May 17, 2012, 6:59 PM

Post #1 of 9 (1443 views)
How to sort the following data Can't Post

Hi,

I have a file as follows:

John 35939758
John 35941793
John 35943998
Amy 11920824
Peter 26592418
Peter 26605776
Peter 26658615
Mary 1698224
Mary 1700964

For each name (the first column), the second column has already been sorted by a numerical order. Now, I want to do a second sort on this file to get the following order:

Mary 1698224
Mary 1700964
Amy 11920824
Peter 26592418
Peter 26605776
Peter 26658615
John 35939758
John 35941793
John 35943998

You can see the second order I want is just to change the group (i.e., name group in the first column) order by applying the numerical order on the group.

Anyone knows how to do that?

Thanks


rovf
Veteran

May 18, 2012, 12:29 AM

Post #2 of 9 (1438 views)
Re: [cliffyiu] How to sort the following data [In reply to] Can't Post

This looks more like a question about an algorithm than about Perl...

I could see several approaches, but the basic idea is always to consider an 'item' to sort not being one line, but one group. A straightforward (not necessarily the best) approach would be to build an auxiliary array, where each array holds one group. For efficiency reason (i.e. if you have to sort more than just a few groups), you might consider extracting the number which is used to be sorted, when building the array. For instance, your aux array could look like this:

[
[35939758,"John 35939758\nJohn 35941793\nJohn 35943998 ],
[11920824,"Amy 11920824\n],
...
]

You then sort the auxiliary array according to the extracted numbers, and create from the sorted array the result data.


FishMonger
Veteran / Moderator

May 18, 2012, 6:36 AM

Post #3 of 9 (1426 views)
Re: [cliffyiu] How to sort the following data [In reply to] Can't Post

What criteria determines the sorting order of column 1?

Rather than using an auxiliary array as rovf suggests, I'd load the data into a hash of arrays where col 1 (the names) would be the keys and the values would be an array of numbers.


FishMonger
Veteran / Moderator

May 18, 2012, 6:57 AM

Post #4 of 9 (1425 views)
Re: [cliffyiu] How to sort the following data [In reply to] Can't Post

Is this all you're looking for?

Code
#!/usr/bin/perl 

use strict;
use warnings;

my @data = <DATA>;
my @sorted = sort { (split ' ', $a)[1] <=> (split ' ', $b)[1] } @data;
print @sorted;


__DATA__
John 35939758
John 35941793
John 35943998
Amy 11920824
Peter 26592418
Peter 26605776
Peter 26658615
Mary 1698224
Mary 1700964


Outputs:

Quote
Mary 1698224
Mary 1700964
Amy 11920824
Peter 26592418
Peter 26605776
Peter 26658615
John 35939758
John 35941793
John 35943998



cliffyiu
Novice

May 18, 2012, 10:16 AM

Post #5 of 9 (1418 views)
Re: [rovf] How to sort the following data [In reply to] Can't Post

Thanks a lot rovf! I am trying to find a "sort" function to solve this problem. If I can't, I will try your suggested algorithm.


cliffyiu
Novice

May 18, 2012, 10:32 AM

Post #6 of 9 (1417 views)
Re: [FishMonger] How to sort the following data [In reply to] Can't Post


In Reply To
What criteria determines the sorting order of column 1?


This is a very good question! Sorry that I didn't clarify it in my first post. I want to sort the first column by group. For example,
if the input is like this (I just changed the value of column 2 for the line of "Amy")


Quote
John 35939758
John 35941793
John 35943998
Amy 35941793
Peter 26592418
Peter 26605776
Peter 26658615
Mary 1698224
Mary 1700964


I want to get the sorted data as


Quote
Mary 1698224
Mary 1700964
Peter 26592418
Peter 26605776
Peter 26658615
John 35939758
John 35941793
John 35943998
Amy 35941793


As you can see, now the line of "Amy" is at the bottom because 35941793 is smaller than 35939758 (the first value for the group of "John").

But if I use your suggested code, the output will be



Quote
Mary 1698224
Mary 1700964
Peter 26592418
Peter 26605776
Peter 26658615
John 35939758
John 35941793
Amy 35941793
John 35943998


I understand it's because the second column is now sorted by a numerical order but I want the "Amy" line (or lines) can be taken out of the "John" group.

Is it still possible to use some kind of simple "sort" function to do that?

Thanks again!


FishMonger
Veteran / Moderator

May 18, 2012, 11:10 AM

Post #7 of 9 (1411 views)
Re: [cliffyiu] How to sort the following data [In reply to] Can't Post

In your example, why should the "Amy" line be at the end?


cliffyiu
Novice

May 18, 2012, 11:48 AM

Post #8 of 9 (1409 views)
Re: [FishMonger] How to sort the following data [In reply to] Can't Post

Oh, as I said in the last post, I just want it to be at the bottom because 35941793 is smaller than 35939758 (the first value for the group of "John").


rovf
Veteran

May 19, 2012, 3:23 AM

Post #9 of 9 (1367 views)
Re: [FishMonger] How to sort the following data [In reply to] Can't Post

I think, the idea of this sorting is, that each group (a group being all lines with the same name) has one "pivot" number, which is the smallest number within this group. The groups then need to be sorted ascending according to their pivot number. Within each group, the lines are simply sorted ascending to their numbers.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives