Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Sorting in Perl

by perlUser345 (Acolyte)
on Oct 15, 2015 at 00:02 UTC ( [id://1144918]=perlquestion: print w/replies, xml ) Need Help??

perlUser345 has asked for the wisdom of the Perl Monks concerning the following question:

I have a list of data points like this:
100, 10, y
100, 9, n
90, 9, y
90, 8, n
(and so on)

So the first column is from 10 to 100 (in this case), the second from 1-10 in this case and the thrid a y or n. I want to sort them by the first column in descending order but I want the respective second and third column to go with it. So the result should look like this:
100, 10, y
100, 9, n
100, 8, n
100, 7, y
100, 6, y
(skipping a few here)
90, 10, y
90, 9, y
90, 8, n
(and so on)

Thanks!

Update: Thank you all! Thank you AppleFritter, that worked like a charm!

Replies are listed 'Best First'.
Re: Sorting in Perl
by dasgar (Priest) on Oct 15, 2015 at 04:08 UTC

    If you would like to use a module to do this, you can check out Data::Table. It provides a function that lets you sort by one or more column and use different sort orders for each column.

    Alternatively, you may prefer to write your own code to do the sorting. You can check out How to sort array by columns maintaining the rows? (for C style arrays) for some suggestions on how to write your own code.

    I've used both of the above methods to accomplish the task that you are trying to do.

Re: Sorting in Perl
by AnomalousMonk (Archbishop) on Oct 15, 2015 at 01:51 UTC

    See also A Fresh Look at Efficient Perl Sorting

    I think you also need to be more clear about exactly what is in your list. Is it strings:

    my @list = ( '100, 10, y', '100, 9, n', ..., '90, 8, n', ..., );
    or arrays:
    my @list = ( [ 100, 10, y ], [ 100, 9, n ], ..., [ 90, 8, n ], ..., );
    I frequently see  'x, y, z' referred to as a "list" or "array". It's a string.


    Give a man a fish:  <%-{-{-{-<

Re: Sorting in Perl
by AppleFritter (Vicar) on Oct 15, 2015 at 10:17 UTC

    Really depends on what your data structure looks like. For instance, if you've got an array of arrays (i.e. array references), you could do the following:

    my @sorted = sort { $b->[0] <=> $a->[0] || $b->[1] <=> $a->[1] || $b->[2] cmp $a->[2] } @list;

    If you've got an array of strings of the form "100, 10, y", use a Schwartzian Transform (Wikipedia has more) to be able to use the above:

    my @sorted = map { join ",", @$_ } sort { $b->[0] <=> $a->[0] || $b->[1] <=> $a->[1] || $b->[2] cmp $ +a->[2] } map { my @t = split /,/, $_; \@t } @list;

    EDIT:

    Update: Thank you all! Thank you AppleFritter, that worked like a charm!

    You're welcome, partner! *tips hat*

      Thank you! That worked great!
      there are many different ways in perl to sort numbers, i found those here
      use strict; use Data::Dumper; #Remove duplicates from array. my @array = qw/10 20 20 20 30 40 40 40 50 50 50/; print "\n Duplicate array: @array"; ##1) Good my %hash; $hash{$_} = 0 for (@array); # $hash{$_} = () for (@array); #You can do this also my @final = keys (%hash); print "\n Unique Array: @final"; print "\n"; ##2) Best of all my %hash = map { $_ , 1 } @array; my @uniq = keys %hash; print "\n Uniq Array:", Dumper(\@uniq);
      Source: Different ways to remove duplicates from array - sort and uniq in perl

        Those don't actually sort a list, they remove duplicate entries from it.

        Another variant on the same theme is using a slice, e.g.:

        my @array = (10 20 20 20 30 40 40 40 50 50 50); my %hash; @hash{ @array } = (); say keys %hash;

        But what I'd personally recommend is using the uniq function from List::MoreUtils (or List::AllUtils if you can't be bothered to remember whether functions are List:Util or List:MoreUtils).

        It avoids reinventing the wheel (fun as it is to do so as an academic exercise), it's shorter, and it makes it crystal clear what's going on, which will benefit whoever reads your code at a later time.

        EDIT: changed @hash{ @array } = 0 to @hash{ @array } = (); thanks for the suggestion to choroba.

Re: Sorting in Perl
by stevieb (Canon) on Oct 15, 2015 at 00:57 UTC

    You've been around for a little while, I'm sure you know that you need to post what you've attempted.

    This isn't a code writing service, but a service that educates people.

    -stevieb

Re: Sorting in Perl
by Anonymous Monk on Oct 15, 2015 at 00:30 UTC
Re: Sorting in Perl
by RichardK (Parson) on Oct 15, 2015 at 09:54 UTC

    If you're on linux/unix you could use the sort utility which will do that without you writing any code at all.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1144918]
Approved by TStanley
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2024-03-28 23:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found