Partial Sort ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • comatose

    Partial Sort ?

    if I have an array of ten items how can I sort the first five items in
    the array without sorting the second five items?
  • Geoff Berrow

    #2
    Re: Partial Sort ?

    Message-ID: <7ikh73p8vre66r j7tjhqkauovfku9 jct4j@4ax.comfr om comatose
    contained the following:
    >if I have an array of ten items how can I sort the first five items in
    >the array without sorting the second five items?
    Use a loop to copy first five to a temporary array, sort that array and
    then replace into original using array_splice()
    --
    Geoff Berrow (put thecat out to email)
    It's only Usenet, no one dies.
    My opinions, not the committee's, mine.
    Simple RFDs http://www.ckdog.co.uk/rfdmaker/

    Comment

    • =?ISO-8859-15?Q?Iv=E1n_S=E1nchez_Ortega?=

      #3
      Re: Partial Sort ?

      comatose wrote:
      if I have an array of ten items how can I sort the first five items in
      the array without sorting the second five items?
      By using an user-defined sorting function.

      See http://php.net/usort

      You'll need to do some dirty tricks in that function to keep track of how
      many items you have sorted, to not sort those, but that should be the way..

      --
      ----------------------------------
      Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

      http://acm.asoc.fi.upm.es/~mr/ ; http://acm.asoc.fi.upm.es/~ivan/
      MSN:i_eat_s_p_a _m_for_breakfas t@hotmail.com
      Jabber:ivansanc hez@jabber.org ; ivansanchez@kde talk.net

      Comment

      • Lars Eighner

        #4
        Re: Partial Sort ?

        In our last episode, <7ikh73p8vre66r j7tjhqkauovfku9 jct4j@4ax.com>, the
        lovely and talented comatose broadcast on comp.lang.php:
        if I have an array of ten items how can I sort the first five items in
        the array without sorting the second five items?

        Try searching the function index of manual for 'array' --- I'm pretty sure
        you will find some useful slices of information to splice into your
        knowledge base.

        --
        Lars Eighner <http://larseighner.com/ <http://myspace.com/larseighner>
        Countdown: 580 days to go.
        Owing to googlegroups not screening users to eliminate spammers and other
        USENET abusers, I do not see most posts from googlegroups.

        Comment

        • petersprc

          #5
          Re: Partial Sort ?

          On Jun 20, 3:18 am, comatose wrote:
          if I have an array of ten items how can I sort the first five items in
          the array without sorting the second five items?
          $array = array('c', 'b', 'a', 'q', 's', 'r', 'h', 'p');
          $head = array_slice($ar ray, 0, 5);
          sort($head);
          $newArray = array_merge($he ad, array_splice($a rray, 5));

          I believe someone mentioned this...

          Comment

          Working...