0

こんにちは、日付別、カテゴリ別、特定の順序での重量別など、さまざまなキーで多次元配列をソートする際に問題に直面しています。

mysql の出力配列 (データ) に厳しいビジネス ロジックを実装する必要があるため、これらの配列を mysql の機能ごとに注文することはできません。

ビジネスロジックを実装した後、ソートする必要がある次のタイプの配列を見つけました

日付の昇順、カテゴリの降順、体重の昇順。

配列のサイズは 10000 以上です。

私はすでに usort 関数を使用していますが、並べ替え要素の値が同じ場合の固定順序の問題を解決できません。

助けてください。

Array
(
    [0] => Array
        (
            [categorie] => xyz
            [date] => 2012-12-08 19:30
            [weight] => 3
            [row_id] => 125812
            [searchtype] => show
            [uitgespeeld] => 0
        )

[1] => Array
    (
        [categorie] => show
        [date] => 2012-12-10 20:15
        [weight] => 3
        [row_id] => 125816
        [searchtype] => show
        [uitgespeeld] => 0
    )

[2] => Array
    (
        [categorie] => abc
        [date] => 2012-12-13 20:30
        [weight] => 3
        [row_id] => 119151
        [searchtype] => show
        [uitgespeeld] => 0
    )
   .......

)

ソートに使用したコード。

usort($temp_group_data, array('className','cmp_weight'));
usort($temp_group_data, array('className','cmp_date'));

function cmp_weight($a, $b) {
    if (($a['weight']==$b['weight']) ) {
        return 0;
    } else if ($a['weight'] >$b['weight']) {
        return -1;
    } else {
        return 1;
    }
}

function cmp_date($a, $b) {
    if (($a['date']==$b['date']) ) {
        return 0;
    } else if (strtotime($a['date']) >strtotime($b['date'])) {
        return -1;
    } else {
        return 1;
    }
}
4

2 に答える 2

2

1 つの関数で行う必要があります。2 番目の並べ替えは、最初に行われた変更を上書きします。

function multicompare($a,$b){
    $criteria = array(
        'date' => 'asc',
        'category' => 'desc',
        'weight' => 'asc'
    );
    foreach($criteria as $what => $order){
        if($a[$what] == $b[$what]){
            continue;
        }
        return (($order == 'desc')?-1:1) * strcmp($a[$what], $b[$what]);
    }
    return 0;
}
于 2012-12-02T20:32:56.167 に答える
0

The problem is two-fold, judging by the last part of your question:

  1. All conditions must be evaluated at the same time rather than consecutively.

  2. You need stable sorting to retain the ordering in case two values are the same (i.e. the original order)

Both steps in one goes like this; first you "decorate" the array using the index in which they appear in the original array:

foreach ($a as $key => &$item) {
    $item = array($item, $key); // add array index as secondary sort key
}

usort($a, 'mysort'); // sort it

// undecorate
foreach ($a as $key => &$item) {
    $item = $item[0]; // remove decoration from previous step
}

And here's the all-in-one sorting function:

function mysort($a, $b)
{
    if ($a[0]['date'] != $b[0]['date']) {
            return $a[0]['date'] < $b[0]['date'] ? -1 : 1; // ASC
    } elseif ($a[0]['category'] != $b[0]['category']) {
            return $a[0]['category'] < $b[0]['category'] ? 1 : -1; // DESC
    } elseif ($a[0]['weight'] != $b[0]['weight']) {
            return $a[0]['weight'] < $b[0]['weight'] ? -1 : 1; // ASC
    } else {
            return $a[1] < $b[1] ? -1 : 1; // ASC
    }
}
于 2012-12-03T02:27:30.483 に答える