RE: [PHP] Sorting arrays [SOLVED I THINK]

From: Jake McHenry (linux_at_nittanytravel.com)
Date: 12/14/03


Date: Sun, 14 Dec 2003 03:22:49 -0500


> -----Original Message-----
> From: Jake McHenry [mailto:linux@nittanytravel.com]
> Sent: Sunday, December 14, 2003 3:14 AM
> To: 'Justin Patrin'
> Cc: 'Php-general'
> Subject: RE: [PHP] Sorting arrays
>
>
> > -----Original Message-----
> > From: Justin Patrin [mailto:papercrane@reversefold.com]
> > Sent: Sunday, December 14, 2003 2:55 AM
> > To: php-general@lists.php.net
> > Subject: Re: [PHP] Sorting arrays
> >
> >
> > Jake McHenry wrote:
> >
> > >>-----Original Message-----
> > >>From: Bronislav Klucka [mailto:Bronislav.Klucka@pro2-soft.com]
> > >>Sent: Sunday, December 14, 2003 2:22 AM
> > >>To: Jake McHenry
> > >>Cc: 'Php-general'
> > >>Subject: RE: [PHP] Sorting arrays
> > >>
> > >>
> > >>
> > >>>>Try to explain a little bit more how the arrays could be
sorted
> > >>>>asscending and by hitting back and submitting form
> > >>
> > >>suddnely sorted
> > >>
> > >>>>descending...
> > >>>>
> > >>>>
> > >>>>Brona
> > >>>>
> > >>>>
> > >>>>>Does anyone know of a way to keep sort from reversing the
> > >>>>
> > >>>>array order?
> > >>>>
> > >>>>>That is kinda fuzzy, let me explain.
> > >>>>>
> > >>>>>I have a couple arrays that I am sorting. If the user hits
the
> > >>>
> > >>>back
> > >>>
> > >>>>>button in their browser, and hits submit again without
> > >
> > > changing
> > >
> > >>>>>anything, the array is sorted again, now in decending order
> > >>>>
> > >>>>instead of
> > >>>>
> > >>>>>ascending as it was the first time around.
> > >>>>>
> > >>>>>The arrays contain dates, and I want them in ascending order.
> > >>>>>
> > >>>>>Is there a way to limit any of the sort routines to ascending
> > >>>
> > >>>only?
> > >>>
> > >>>>>Thanks,
> > >>>>>
> > >>>>>Jake McHenry
> > >>>>>Nittany Travel MIS Coordinator http://www.nittanytravel.com
> > >>>>>
> > >>>>>--
> > >>>>>PHP General Mailing List (http://www.php.net/)
> > >>>>>To unsubscribe, visit: http://www.php.net/unsub.php
> > >>>>>
> > >>>>
> > >>>This page is for adding / editing / removing airline
> reservations.
> > >
> > >
> > >>>What I have done so far is when the user first gets to this
> > >>
> > >>specific
> > >>
> > >>>page, they enter their info. If they put a check in the add
next
> > >>>flight checkbox, then the same form comes up again, each
> > >>
> > >>time adding
> > >>
> > >>>the new info to the same array, building upon the old info.
> > >>
> > >>The arrays
> > >>
> > >>>are then displayed back into form fields where the user can
> > >>
> > >>edit any
> > >>
> > >>>info submitted.
> > >>>
> > >>>I'm sorting the arrays by the flight dates. Each time the
> > >>
> > >>user enters
> > >>
> > >>>a new flight, the array is sorted by date. This is where I'm
> > >
> > > seeing
> > >
> > >>>the problem. The first flight is fine, of course. The
> > >>
> > >>second is fine.
> > >>
> > >>>The third flight entered and all of the flights are now in
> reverse
> > >
> > >
> > >>>order. Then the next flight entered, they're all in the
> > >>
> > >>correct order,
> > >>
> > >>>etc, etc.
> > >>>
> > >>>This problem will most likely happen to my other arrays
> > >>
> > >>further on in
> > >>
> > >>>the project.
> > >>>
> > >>>Is there any way I can make sure that the dates are
> always in the
> > >>>correct order?
> > >>>
> > >>
> > >>I'm pretty confused.... so user puts information into
> form, he/she
> > >>submits it, and you have 1 date, he press back button,
> fill the same
> > >>form again submits form, new date is added and it's in correct
> > >>order, he/she does it for the third and it's in reverse order,
> > >>he/she does it for the fourth and it's in correct
> order... funny...
> > >>could you paste here the sorting part of code?
> > >>
> > >>
> > >>Brona
> > >>
> > >
> > >
> > > The user doesn't even have to hit back, each time the form is
> > > submitted, the data is rearranged in opposite order. Too
> > keep my data
> > > together, here is what the data in the arrays looks
> > > like:
> > >
> > > Array
> > > (
> > > [0] => 01-30-2004|Wilkes-Barre|PA|Salt Lake|UT||Yes|Yes
> > > [1] => 01-16-2004|Wilkes-Barre|PA|Salem|WA||Yes|Yes
> > > [2] => 01-02-2004|Wilkes-Barre|PA|Denver|CO||Yes|Yes
> > > [3] => 01-08-2004|Denver|CO|Wilkes-Barre|PA||No|No
> > > [4] => 01-23-2004|Salem|WA|Wilkes-Barre|PA||No|No
> > > [5] => 02-11-2004|Salt Lake|UT|Wilkes-Barre|PA||No|No
> > > )
> > >
> > >
> > > Then, in my script, after the new data is merged with the
existing
>
> > > array, I sort:
> > >
> > > $array = $_SESSION['Air_Reservations'];
> > > sort($array)
> > >
> > > New output:
> > >
> > > Array
> > > (
> > > [0] => 02-11-2004|Salt Lake|UT|Wilkes-Barre|PA||No|No
> > > [1] => 01-23-2004|Salem|WA|Wilkes-Barre|PA||No|No
> > > [2] => 01-08-2004|Denver|CO|Wilkes-Barre|PA||No|No
> > > [3] => 01-02-2004|Wilkes-Barre|PA|Denver|CO||Yes|Yes
> > > [4] => 01-16-2004|Wilkes-Barre|PA|Salem|WA||Yes|Yes
> > > [5] => 01-30-2004|Wilkes-Barre|PA|Salt Lake|UT||Yes|Yes
> > > )
> > >
> > >
> > > See now? I didn't add a new record this time, but it
> > happens with our
> > > without adding anything new. Each time the array is sorted, this
> > > happens.
> > >
> > >
> > >
> > > Thanks,
> > > Jake
> >
> > It may be that because those are strings they are being sorted
> > strangely. I have two suggestions.
> >
> > 1) Use a sub-associative array or an object for those
> > entries. Delimited
> > strings are ok, but actual structures are generally better.
> >
> > 2) use usort() and specify a function which sorts those dates
> > correctly
> > (sorting those won't sort by year, only month). OR you could
convert
>
> > those dates to UNIX timstamps (with strtotime() possibly) and
> > sort them
> > that way (this may also require using usort()).
> >
> > Try making an array with ONLY dates in it (not the extra
> > info) and see
> > if it sorts those correctly.
> >
> > One more thing: how are you passing these values between pages?
> >
> > --
> > paperCrane <Justin Patrin>
> >
> > --
> > PHP General Mailing List (http://www.php.net/)
> > To unsubscribe, visit: http://www.php.net/unsub.php
> >
>
> Ok, built the test array, here's the code and results:
>
> foreach ($_POST['Change_Date'] as $key => $value)
> {
> $build = "{$_POST['Change_Date'][$key]}|";
> $build .= "{$_POST['Change_Departure_City'][$key]}|";
> $build .= "{$_POST['Change_Departure_State'][$key]}|";
> $build .= "{$_POST['Change_Destination_City'][$key]}|";
> $build .= "{$_POST['Change_Destination_State'][$key]}|";
> $build .=
> "{$_POST['Change_Preferred_Flight_Times'][$key]}|";
> $build .= "{$_POST['Change_Need_Car'][$key]}|";
> $build .= "{$_POST['Change_Need_Hotel'][$key]}";
> $old = array_merge($build, $old);
>
> $test = "{$_POST['Change_Date'][$key]}";
> $array = array_merge($test, $array);
> }
> $_SESSION['Air_Reservations'] = $old;
> }
> print_r($array);
>
>
> I'm seeing that just the dates are doing the exact same thing,
without
> even being sorted. Could this be because of the foreach loop?
>
> The sorting function call is called later after all the arrays are
> built, and I'm seeing the problem before sorting happens.
>
> What is causing this, and why isn't the sorting function working..
> Since it's called later after the arrays are built? Even if this
> problem existed, the when sort is called, shouldn't it sort the data
> into the correct order?
>
> Thanks,
> Jake
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

It was the way I was using array_merge. I was merging the old data
with the new data, I switched the order of the merge and the problem
seems to have gone away. I'm very tired.. Sorry about all this. Guess
I just needed to talk my way through it.

Thanks,

Jake McHenry
Nittany Travel MIS Coordinator
http://www.nittanytravel.com



Relevant Pages

  • RE: [PHP] Sorting arrays
    ... > built, and I'm seeing the problem before sorting happens. ... and why isn't the sorting function working.. ... > problem existed, the when sort is called, shouldn't it sort the data ... It seems that the POST array is being sorted? ...
    (php.general)
  • Re: Help with Array(s)
    ... I would think XML would return in something with an XML schema and you could ... Array.Sort sorts one-dimentional arrays, but don't think it will 2dim arrays ... to sort list by second int value and display. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Efficient python 2-d arrays?
    ... I can create large 2-dimensional arrays quite easily. ... my target audience may not have numpy so I'd prefer not to use it. ... Since I want to keep the two elements together during a sort, ... Use a list of lists, where the outer list is the easier one to sort ...
    (comp.lang.python)
  • Re: does C# have any collection objects that support sort functionality so that I dont have to writ
    ... the IComparable interface. ... Also, since arrays are strongly typed, you know that all elements are of the ... for ArrayLists. ... The Sort() method will utilize each object's IComparable ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: A Sorting Puzzle
    ... Rows and columns of tables, although indexable, are not arrays, and have ... Since the elements can be both read and written, all normal sort ... generate directly comparable sort keys. ... That's easy in JavaScript. ...
    (comp.lang.javascript)