PHP中的usort() 函数是非稳定排序怎么办?
项目有个需求
一组数据,先按参数1进行排序,参数1相同的,再按参数2进行排序。
如果usort是稳定排序的话,直接用usort把数组按参数2进行排序,再把排序后的数组按参数1进行排序。
但是经过试验,usort竟然是不稳定排序,怎么办?
代码如下:
<?php$a1 = array('key1'=>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;}?>
输出如下:
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 ))
这种情况下,只能自己写个插入排序之类的稳定排序算法了吗?
回复讨论(解决方案)
$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 ))
多维数组排序用这个array_multisort
http://php.net/manual/zh/function.array-multisort.php
<?php$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){ $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
)
)
多谢两位指点 @xuzuning @傲雪星枫

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Following its high-profile acquisition by Facebook in 2012, Instagram adopted two sets of APIs for third-party use. These are the Instagram Graph API and the Instagram Basic Display API.As a developer building an app that requires information from a

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio
