Hello.
halojoy here.

There are like 10 functions in PHP that can sort an array.
In different ways.
I hope you can advice me what to use in this case.

But also we can discuss some other cases dealing with Sorting Arrays.
Can be very useful to know, as a row, record, from database
most likely turns up as An Array of some kind.

<?php

// Original data
// name, age, location, email
$data[0] = array( 'billy', 33, 'UK', 'billy@londontown.com' );
$data[1] = array( 'sally', 74, 'Germany', 'sally@yahoo.com' );
$data[2] = array( 'ronny', 17, 'USA', 'ronny@google.com' );
$data[3] = array( 'tim', 50, 'Singapore', 'tim@hotmail.com' );

// Most simple way to do this task below?
// Must be several ways to re-sort this data array.
// Several php functions can do same thing.
// Give me 2-3 different solutions
// I am sure persons would come up with own different way to do this
// But one has to be best one! The one I will use.


// Question 1: How to sort this array after column 'age'?
// Result order should be: 17-33-50-74
$data[0] = array( 'ronny', 17, 'USA', 'ronny@google.com' );
$data[1] = array( 'billy', 33, 'UK', 'billy@londontown.com' );
$data[2] = array( 'tim', 50, 'Singapore', 'tim@hotmail.com' );
$data[3] = array( 'sally', 74, 'Germany', 'sally@yahoo.com' );

// Question 2: How to sort by email provider?
// Result order should be: google-hotmail-londontown-yahoo
$data[0] = array( 'ronny', 17, 'USA', 'ronny@google.com' );
$data[1] = array( 'tim', 50, 'Singapore', 'tim@hotmail.com' );
$data[2] = array( 'billy', 33, 'UK', 'billy@londontown.com' );
$data[3] = array( 'sally', 74, 'Germany', 'sally@yahoo.com' );


// Question 3/4: How to sort like 1 and 2, but with reverse order?
// Result order should be, age: 74-50-33-17
// Result order should be, email: yahoo-londontown-hotmail-google

// Thanks for your help. Next time I help you.
//   /halojoy July 2005 Sweden

?>
    function sort_age_asc($a, $b) {
    	if ($a[1] > $b[1])
    		return 1;
    	return -1;
    }
    
    function sort_age_desc($a, $b) {
    	if ($a[1] < $b[1])
    		return 1;
    	return -1;
    }
    
    function sort_mail_asc($a, $b) {
    	$a = end(explode('@', $a[3]));
    	$b = end(explode('@', $b[3]));
    	if ($a > $b)
    		return 1;
    	return -1;
    }
    
    function sort_mail_desc($a, $b) {
    	$a = end(explode('@', $a[3]));
    	$b = end(explode('@', $b[3]));
    	if ($a < $b)
    		return 1;
    	return -1;
    }
    usort($data, 'sort_mail_asc');
    print_r($data);

    hth

      thanks
      mrhappiness
      I thought there were more ways than one
      but I guess I was all wrong.

      Still a bit unsure how to do this sort thing in best, most effective, and fast way
      I will have to wait
      until more '1000 posts' experts arrive, to tackle this delicate issue
      of sorting an array using php functions

      :o

        If there was one "best, most effective, and fast way" to sort an array, there would only be one way to sort an array. The reason there are different ways to sort the array is because there are different ways you might want to sort the array.

        Use whichever function expresses your intentions the best. They all use the same algorithm (quicksort) inside anyway.

          🙂

          I saw in another thread in 'Coding' that


          [man]uksort[/man]

          WAS FASTEST sorting method in php
          ( at least by timing an example like mine )

          how can I use uksort

          • 5. In my my first example question 1. ( by age )
          • 6. In my my first example question 2. ( by email provider)
            -- 7. In my my first example question 3. ( age reversed )
            -- 8. In my my first example question 4. ( email reversed )


          SPEED is real an issue for me here!
          as there can be several 1000 records in Array: $data

          Thanks for helping me,
          as I would help you, if only I can with what little I know

          /halojoy
          sweden July 2005

            SPEED is real an issue for me here!

            if speed is your real issue you should stop playing with text files (as you always seem to mention) and move on to a proper data storage method.

            databases are made for storing / sorting data.

              Actually this Fast and Effective sorting function
              is related to myMy PHP Mini DataBase Class Script ( see my signature)

              so
              every microsecond I can gain is of importance
              optimized functions
              I want my scripts as perfect as can ever be

              Lyrics:
              its got to be-e-e-e-e-e-e
              perfect
              its got to be-e-e-e-e-e-e
              worth it,
              yeah
              too many people take second best
              but I wont take anything less
              its got to be-e-e-e-e-e-e
              Yeah!
              perfect

              'Perfect' /Fairground Attraction
              U.K. Chart: #1 (1988)

              /when halojoy was a younger man - something he will never be again

              🙂

                uksort does the same as usort but acts on the array keys rather than the data. None of your data has any of the fields you'd want to sort on in the keys so it can't be done.

                  If speed is your primary concern then you can't do better than getting in some serious hardware and a nice big pipe.

                    Write a Reply...