What should I do if the usort () function in PHP is non-stable sorting?

Source: Internet
Author: User
What should I do if the usort () function in PHP is non-stable sorting? Project requirements
A group of data is first sorted by parameter 1, and then by parameter 2 if parameter 1 is the same.
If usort is a stable sort, use usort to sort the array by parameter 2, and then sort the sorted array by parameter 1.
However, after the test, the usort sorting is unstable. what should I do?
The code is as follows:
 1,'key2'=>1);$a2 = array('key1'=>2,'key2'=>2);$a3 = array('key1'=>2,'key2'=>3);$a = array($a3,$a2,$a1);usort($a,"my_sort2");print_r($a);usort($a,"my_sort1");print_r($a);function my_sort1($a,$b){if ($a['key1']==$b['key1']) return 0;return ($a['key1']<$b['key1'])?-1:1;}function my_sort2($a,$b){if ($a['key2']==$b['key2']) return 0;return ($a['key2']<$b['key2'])?-1:1;}?>

The output is as follows:
Array(    [0] => Array        (            [key1] => 1            [key2] => 1        )    [1] => Array        (            [key1] => 2            [key2] => 2        )    [2] => Array        (            [key1] => 2            [key2] => 3        ))Array(    [0] => Array        (            [key1] => 1            [key2] => 1        )    [1] => Array        (            [key1] => 2            [key2] => 3        )    [2] => Array        (            [key1] => 2            [key2] => 2        ))

In this case, can I write a stable sorting algorithm like insert sorting?


Reply to discussion (solution)

$a1 = array('key1'=>1,'key2'=>1);$a2 = array('key1'=>2,'key2'=>2);$a3 = array('key1'=>2,'key2'=>3);$a = array($a3,$a2,$a1);foreach($a as $v) {  $r2[] = $v['key2'];  $r1[] = $v['key1'];}array_multisort($r1,$r1, $a);print_r($a);
Array(    [0] => Array        (            [key1] => 1            [key2] => 1        )    [1] => Array        (            [key1] => 2            [key2] => 2        )    [2] => Array        (            [key1] => 2            [key2] => 3        ))

Use array_multisort to sort multi-dimensional arrays.
Http://php.net/manual/zh/function.array-multisort.php

 1,'key2'=>1);$a2 = array('key1'=>2,'key2'=>2);$a3 = array('key1'=>2,'key2'=>3);$a = array($a3,$a2,$a1);foreach($a as $v){     $ar1[] = $v['key1'];     $ar2[] = $v['key2'];}array_multisort($ar1,SORT_ASC,$ar2,SORT_ASC, $a);print_r($a);?>



Array
(
[0] => Array
(
[Key1] => 1
[Key2] => 1
)

[1] => Array
(
[Key1] => 2
[Key2] => 2
)

[2] => Array
(
[Key1] => 2
[Key2] => 3
)

)

Thank you for your guidance. @ xuzuning @ Aoxue Xingfeng

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.