Re: Sorting based on string value



Ninja67 wrote:
I've got the following code in a script:

my $cost_order = "A2yB";
my @series_cost = ();

for (@{$struct->{tree}->[0]->{rectangle}}) {
my $rec_string = "<table><tr><td ";
$rec_string .= ' meascode="'.$_->{meascode}.'"></td></tr></table>';

SWITCH: {
if ($_->{datafile} =~ /^cost/) { push(@series_cost, $rec_string);
last SWITCH; }
$nothing = 1;
}
}

Now that I have my array filled, I want to sort the contents of each
array by the "meascode" attribute. Each meascode attribute is a single
character that matches one of the characters in the $cost_order string.
How can I sort an array based on the order of the characters in the
$cost_order string?

So my array might contain:
<table><tr><td meascode="y"></td></tr></table>
<table><tr><td meascode="2"></td></tr></table>
<table><tr><td meascode="A"></td></tr></table>
<table><tr><td meascode="B"></td></tr></table>

but I need it to contain:
<table><tr><td meascode="A"></td></tr></table>
<table><tr><td meascode="2"></td></tr></table>
<table><tr><td meascode="y"></td></tr></table>
<table><tr><td meascode="B"></td></tr></table>

Maybe I'm not understanding your goal, but why aren't you just sorting
the values in your original array, before looping through them and
putting them inside strings in a new array? Why would you want to fill
the array, convoluting your value, then have to loop back through that
string, parse out the data you just put in, so that you can compare it?

Change:
for (@{$struct->{tree}->[0]->{rectangle}}) {
To:
for (sort { $a->{meascode} cmp $b->{meascode} }
@{$struct->{tree}->[0]->{rectangle}}) {

Paul Lalli

.



Relevant Pages

  • Re: Sort Type
    ... Like I said, the Space$ function itself it is about 50 per cent faster than the equivalent String$ function, but the main speed improvement will come from minimizing the number of times you call your Normalize function by taking it out of the sort routine itself. ... I can't really comment further on your normalize function since you have not provided a comprehensive sample of the stuff you intend to sort but, whatever function you end up using, my own previous suggestion of normalizing your strings into a temporary array just once and then performing your ShuttleMerge Index Sort on the temporary array will still produce a massive speed increase regardless of what you end up with in your Normalize function. ...
    (microsoft.public.vb.general.discussion)
  • sort 1-D array of doubles for Olaf Schmidt
    ... A few weeks Olaf Schmidt posted a VB6 routine to sort a 1-D array of strings very fast indeed. ... fastest way to change case of string. ... Dim i As Long, j As Long, Lo As Long, Hi As Long, StPtr As Long ...
    (microsoft.public.vb.general.discussion)
  • Re: Collection Structure
    ... property StringID you want ... where inarr is the String array and outarr is an array of longs containing ... needs to sort the input array keeping track of the original position (I use ...
    (microsoft.public.vb.com)
  • Help in French|Spanish|German translation.
    ... I am also an author of User-defined string functions. ... WORDTRANEX (cSearched, cArExpressionSought | cExpressionSough, ... each string of the array is searched ... If the parameter nArStartOccurrence is -1 or omitted, the replacement starts ...
    (microsoft.public.fox.helpwanted)
  • Re: How to sort String array A based on int array B
    ... So I have two arrays: A and B. Array A is an int array (I think I ... it backwards in my original description) and array B is a String ... Modify any of the sort algorithms to sort a secondary array B based on ... I chose to add Artist info in addition to song title. ...
    (comp.lang.java.help)