• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2369
  • Last Modified:

Sorting two columns in a csv file

What is the best way to sort on two columns in a .csv file? Here is an example:

serverone, a1
serverone, cpu
serverone, a2
servertwo, cpu
servertwo, a2
servertwo, a1
etc....

What I would like to do is sort it like so:

serverone, a1
serverone, a2
serverone, cpu
servertwo, a1
servertwo, a2
servertwo, cpu
etc...

I got the first column using 'sort', but can't figure out the best way to sort the second.

Thanks!
David
0
David Aldridge
Asked:
David Aldridge
4 Solutions
 
Frosty555Commented:
You could concatenate the contents of the two columns together and sort on that.

Or, probably the more correct thing to do would be to perform your comparison on the first column, just like you're doing now, but in the event that they are equal, do a comparison on the second column as the tiebreaker.
0
 
Frosty555Commented:
Perl's Sort() function can be passed the name of a user defined subroutine that it will call to perform the comparison, if none of perl's default comparison methods will work in this case. The user defined subroutine is passed the two parameters $a and $b and it is the subroutine's job to compare them and return -1, 0 or 1 based on if $a is less than, equal to or greater than $b.

Basic Sorting With Sort
     ex:
         @x = ("the", "bob", "Bob");
         @y = sort (@x);           # @y is ("Bob", "bob", "the")

Advanced Sorting With Sort
- The subroutine is written to compare the two scalar values,
       $a and $b, and return the following:
           -1 if $a should appear BEFORE $b in the sorted list
            0 if $a and $b are equal in the sort order
            1 if $a should appear AFTER $b in the sorted list

   e.g:

    sub by_number_ascending
       {
         $a <=> $b;
       }

     @x = (25, 14, -3);
     @y = sort (by_number_ascending @x);             # @y is (-3, 14, 25)


So you'd write your subroutine to compare the first column, then, if they're equal, compare the second column for the tiebreaker.

http://www.research.umbc.edu/~tarr/perl/perl4/ch17-sorting.html
0
 
Adam314Commented:
print map {"$_->[0],$_->[1]"} sort {($a->[0] cmp $b->[0]) or ($a->[1] cmp $b->[1])} map{[split /,\s*/]} <>;
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
mjcoyneCommented:
#!/usr/bin/perl -w
use strict;
my %server_data;

while (<DATA>) {
    next if /^\s*$/;
    chomp;
    my ($server, $data) = split /,\s*/;
    $server_data{$server}{$data}++;
}

foreach my $server (sort keys %server_data) {
    foreach my $data (sort keys %{$server_data{$server}}) {
        print "$server, $data\n";
    }
}

__DATA__
serverone, a1
servertwo, cpu
serverone, a2
servertwo, a2
serverone, cpu
servertwo, a1
0
 
ozoCommented:
print sort <DATA>;
__DATA__
serverone, a1
serverone, cpu
serverone, a2
servertwo, cpu
servertwo, a2
servertwo, a1



prints
serverone, a1
serverone, a2
serverone, cpu
servertwo, a1
servertwo, a2
servertwo, cpu

did you want somethinng else?
0
 
David AldridgeAuthor Commented:
Ozo is absolutely correct. After doing some more research, I found out that I had some corrupted data in the csv file which was causing the problem. I apologize for wasting everyone's time. I think the fair thing is to split the points between the four responders.

Thanks everyone,
David
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now