doujiaochan7317 2012-09-28 13:35 采纳率: 0%
浏览 19
已采纳

字母数字数组排序(CAVEAT:空格作为千​​位数字分隔符)

How to do it. For example we have an array:

array(
    0 => 'Regular 1 000',
    1 => 'Regular 500',
    2 => 'Regular 2 000',
    3 => 'Big',
    4 => 'Regular 1 000 000',
    5 => 'Regular 50 000'
)

I need to place them in this order:

array(
    3 => 'Big',
    1 => 'Regular 500',
    0 => 'Regular 1 000',
    2 => 'Regular 2 000',
    5 => 'Regular 50 000',
    4 => 'Regular 1 000 000'
 )

And I need to maintain index associations. It appears I need to use uasort. Could anyone suggest to me a function to use for the callable ( $cmp_function ) value?

  • 写回答

2条回答 默认 最新

  • dqqy64515 2012-09-28 14:39
    关注

    This works only on the numerical part of your array, but I'm still trying to wrap my head around juggling arrays for the alphabetical sorting. Feel free to update this if anyone can add to it.
    You might want to try changing your formatting if possible, because this was a headache.

    $x = array(
        'Regular 1 000',
        'Regular 500',
        'Regular 2 000',
        'Big',
        'Regular 1 000 000',
        'Regular 50 000'
    );
    

    Gives:

    EDIT: Eureka!

    $x = array(
        'A 100', // extra for testing
        'Regular 1 000',
        'Regular 500',
        'Regular 2 000',
        'Big',
        'Regular 1 000 000',
        'Regular 50 000'
    );
    
    function cus_sort($a, $b) { // you can also just do $x = function ($a, $b) {}
        $tmp_a = array(); $tmp_b = array(); // temp storage
        $tmp_a = explode(" ", $a, 2);
        if (@$tmp_a[1]) { // tmp_a exists (suppress invalid offset error if not)
            $a_numeric = (int)preg_replace("/[^0-9]/", '', $tmp_a[1]);
            // remove all non-numerical (...should add hyphens if needed...)
        } else {
            $a_numeric = false; // text only.
        }
        // else make sure that it evaluates false when we check.
        $tmp_b = explode(" ", $b, 2); // split into maximum 2 parts at first space.
        if (@$tmp_b[1]) { // tmp_b exists (suppress invalid offset error if not)
            $b_numeric = (int)preg_replace("/[^0-9]/", '', $tmp_b[1]);
        } else {
            $b_numeric = false; 
        }
        // onwards to sorting
        if ($tmp_a[0] == $tmp_b[0]) { //alphabetical parts are the same.
        // numerical sort
            if (($a_numeric > $b_numeric) || (!$b_numeric)) {
                return 1;
            } elseif (($a_numeric < $b_numeric) || (!$a_numeric)) {
                return -1;
            } else {
                return 0;
            }
        } else {
            // alpha sort
            $compare = strcasecmp($tmp_a[0], $tmp_b[0]);
            // see note below
            if ($compare > 0) {
                return 1;
            } elseif ($compare < 0) {
                return -1;
            } else {
                return 0;
            }
        }
    }
    
    uasort($x, "cus_sort");
    

    Note: strcasecmp(string $a, string $b) is a case insensitive numerical way to check if in order.

    print_r returns:

    Array
    (
        [0] => A 100
        [4] => Big
        [2] => Regular 500
        [1] => Regular 1 000
        [3] => Regular 2 000
        [6] => Regular 50 000
        [5] => Regular 1 000 000
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题