Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Hi Devo, and welcome to PerlMonks.

I think that your specific question can be answered by reading more about sort and reverse. I suspect that your problem is not limited to the title of this node.(Descending Sort)

More generally, here are some obserations about your code.

  • You try to avoid using the special variables '$a' and '$b', since they sometimes have special meaning, especially when dealing with sort; it's good to comment that out.
  • Following a similar strategy, try to keep variables local to the scope, unless there's a good reason not to. For example, your '$sortby' variable appears to have come from outside the function you've written. It is often better, though, to pass the variable as an argument to the function call. Like this:
    my $adata = q(athings1 athings2); my $bdata = q(bthings1 bthings2); my $column = 0; # ... sort_func($column,$adata,$bdata); sub sort_func { my ($sortby,$astring,$bstring) = @_; # this function uses three arguments. }
  • Your function is returning the result of the 'cmp' and '<=>', in a string. The '$result' will then contain -1, 0, or 1. Is that what you wanted? It may be - but then, shouldn't you just split the strings and sort them, dispensing with the need of 'sort_func()'? Then, no more need for non-local variables, that I can see. To learn more about the comparison operators, read perlop.
  • It appears that you are testing equivalence between two values, not actually attempting to sort. Did you mean to write something like this?
    if ($sortby eq "string"){ my $result = sort { $b cmp $a } ($a_items[$item_no], $b_items[$item +_no]); } # $result will contain the greater of the two values.
  • Also, I'm not sure your split will do what you think it will as written. At least, I assume so because you've put a variable called '$datafields' in the 'LIMIT' argument of split. That means that if $datafields is 2, all the columns after the first column will be excluded from split. That's fine if there are only two columns (in which case, leave out the LIMIT), but what if you had three? So, I'd recommend renaming '$datafields' to '$exclude' or something more descriptive of what it does.
  • However, on the assumption that you want to exclude all but a select number of columns of data from split (a good practice), then you might mean to remove that excluded data. I assume this because you seem to want to sometimes sort things numerically - which would never be applicable if the contents of the final element of the array is "111\t222\t333". In other words, if you really meant to limit your split, then you need to jettison the irrelevant data.
    my @a_items = split("\t", $astring,$limit); #store the remainder my $a_remainder = pop @a_items; my @b_items = split("\t", $bstring,$limit); #discard the remainder pop @b_items;

I hope this isn't over-doing the answer (a lot of guessing at your intent) but, it appears that some of these observations might be relevant to why you aren't getting the result that you want.
mkmcconn


In reply to Re: Descending Sort by mkmcconn
in thread Descending Sort by Devo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found