如何实现基于连字符分隔的子字符串的多个自定义排序规则? [英] How to implement multiple custom sorting rules based on hyphen-delimited substrings?

查看:150
本文介绍了如何实现基于连字符分隔的子字符串的多个自定义排序规则?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下格式的文件名数组:

I have an array of filenames of this form:

"A-1.2-平面图.PDF"

"A - 1.2 - Floor Plan.PDF"

我需要首先按照类别按以下顺序对数组进行排序:

I need to sort the array first by the category at the beginning, in the following order:

1. Category: A
2. Category: ESC
3. Category: C
4. Category: M
5. Category: E
6. Category: P

然后,我需要按照类别后面的数字对数组进行排序.

Then I need to sort the array by the numbers following the category.

这是要排序的数组的示例:

Here's an example of the array to be sorted:

$arr[0] = "A - 1.0 - Title Page.PDF";
$arr[1] = "A - 2.2 - Enlarged Floor Plans";
$arr[2] = "A - 2.1.0 - Structural Details.PDF";
$arr[3] = "E - 1.0 - Electrical Title Page.PDF";
$arr[4] = "A - 1.2 - Floor Plan.PDF";
$arr[5] = "P - 1.0 - Plumbing Title Page.PDF";
$arr[6] = "A - 2.1.1 - Structural Details.PDF";
$arr[7] = "C - 1.0 - Civil Title Page.PDF";
$arr[8] = "M - 1.0 - Mechanical Title Page.PDF";
$arr[9] = "ESC - 1.0 - Erosion Control Plan.PDF";

理想情况下,此数组将变为

Ideally, this array would then become

$arr[0] = "A - 1.0 - Title Page.PDF";
$arr[1] = "A - 1.2 - Floor Plan.PDF";
$arr[2] = "A - 2.1.0 - Structural Details.PDF";
$arr[3] = "A - 2.1.1 - Structural Details.PDF";
$arr[4] = "A - 2.2 - Enlarged Floor Plans";
$arr[5] = "ESC - 1.0 - Erosion Control Plan.PDF";
$arr[6] = "C - 1.0 - Civil Title Page.PDF";
$arr[7] = "M - 1.0 - Mechanical Title Page.PDF";
$arr[8] = "E - 1.0 - Electrical Title Page.PDF";
$arr[9] = "P - 1.0 - Plumbing Title Page.PDF";

我具有以下正则表达式,用于适当地对文件名进行分组:

I have the following regular expression for grouping the file names appropriately:

^([A-Z]+?) ?- ?([0-9]+)\.([0-9]+)(\.([0-9]+))?.*$

我想要按组1,按组2,然后按组3排序的数组.如果存在组5,则最后按组5排序.忽略组4.

I want the array sorted by group 1, then by group 2, then by group 3. If Group 5 exists, then sort last by group 5. Ignore group 4.

按字典顺序对类别进行排序可能会更容易.如果是这样,那没关系;尽管按上述顺序对它们进行排序比较可取.

It may be easier to sort the categories lexicographically. If so, that would be alright; though it would be preferable if they were sorted in the order mentioned above.

有什么办法可以用PHP做到这一点?

Is there any way to do this with PHP?

推荐答案

有sort函数,它以compare方法作为参数.您可以像这样使用它:

There is sort function which takes compare method as an argument. You can use it for example like this:

$order = array('A', 'ESC', 'C', 'M', 'E', 'P'); // order of categories
$order = array_flip($order); // flip order array, it'll look like: ('A'=>0, 'ESC'=>1, ...)

function cmp($a, $b)
{
    global $order;

    $ma = array();
    $mb = array();
    preg_match('/^([A-Z]+?) ?- ?([0-9]+)\.([0-9]+)(?:\.([0-9]+))?.*$/', $a, $ma);
    preg_match('/^([A-Z]+?) ?- ?([0-9]+)\.([0-9]+)(?:\.([0-9]+))?.*$/', $b, $mb);

    if ($ma[1] != $mb[1]) {
        return ($order[$ma[1]] < $order[$mb[1]]) ? -1 : 1;
    }
    if ($ma[2] != $mb[2]) {
        return $ma[2] < $mb[2] ? -1 : 1;
    }
    if ($ma[3] != $mb[3]) {
        return $ma[3] < $mb[3] ? -1 : 1;
    }
    // I've changed a regex a little bit, so the last number is 4th group now
    if (@$ma[4] != @$mb[4]) { 
        return @$ma[4] < @$mb[4] ? -1 : 1;
    }
    return 0;
}
usort($arr, "cmp");

这篇关于如何实现基于连字符分隔的子字符串的多个自定义排序规则?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆