如何按字母顺序对 PHP 中的 SplObjectStorage 集进行排序?

How to Alphabetically Sort a SplObjectStorage Set in PHP?

我有一个字母数组,必须使用 rewind()current()next() 等 SPL 方法在 SplObjectStorage() set 中按字母顺序排序, 和 valid()。目前,我的 while 循环无限期地运行而不对任何东西进行排序,但我不确定为什么。我还没有在网上看到任何与排序 SPL 双链表或对象存储相关的信息,所以希望这对 SOC 有用。

<?php

$letters = ["b", "a", "c", "e", "f", "d"];

$setLetters = new SplObjectStorage();

foreach ($letters as $key => $value) {
  $o = new stdClass();
  $o->$key = $value;
  $setLetters->attach($o);
}

function printList($list)
{
  for ($list->rewind(); $list->valid(); $list->next()) {
    $k = $list->key();
    echo ($list->current()->$k);
    echo "<br>";
  }
}

printList($setLetters); // ["b", "a", "c", "e", "f", "d"]

$sortedLetters = $setLetters;

function sortList($list)
{
  $list->rewind();
  $current = $list->current();
  $currentK = $list->key();
  $list->next();
  while ($list->valid()) {
    $nextK = $list->key();
    if ($current->$currentK > $list->current()->$nextK) {
      // [current.element, current.next.element] = [current.next.element, current.element];
      $list->offsetSet($current, $list->current());
      $list->offsetSet($list->current(), $current);
      $list->rewind();
      continue;
    }
    $current = $list->current();
  }
}

sortList($sortedLetters);

printList($sortedLetters); //should return ["a", "b", "c", "d", "e", "f"]

?>

我不认为你可以对存储对象中的对象进行排序 - 至少我没有找到快速修复方法,我认为在一段时间内循环会很昂贵......

这里有一个简单的解决方案:取出所有存储空间,对它们进行分类,然后放回去。

注意:我添加了一些评论让你知道哪里出了问题等等。 如果您可以创建存储,那么我建议使用“属性”或“字母”或 w/e 作为 属性,而不是字母中的当前数字键数组.

注意:我在底部添加了一个带有 属性 ->letter 的解决方案。

// Note: using wild keys for testing reasons.
$letters = [0 => "b", 10 => "a", 22 => "c", 3 => "e", 44 => "f", "d"];

// Create the storage (YOUR VERSION).
// IMPORTANT: you are using $key from the letters array
//            on the new stdClass - that will become a problem.
//            You may want to set them all
//            on the same key (aka property)
//            like f.e. "attribute" or "letter", ... .
$list = new SplObjectStorage();
foreach ($letters as $key => $value) {
    $o = new stdClass();
    $o->{$key} = $value;
    $list->attach($o);
}

# DISABLED - does not work. Reason: $key -problem mentioned above.
#function printList(\SplObjectStorage $list)
#{
#    for ($list->rewind(); $list->valid(); $list->next()) {
#        $k = $list->key();
#        echo($list->current()->$k);
#        echo "<br>";
#    }
#}
/**
 * @param SplObjectStorage $list
 */
function printList(\SplObjectStorage $list)
{
    for (
        $list->rewind(), $i = 0;
        $i < $list->count();
        $i++, $list->next()
    ) {
        // Note: $key is the key from the storage ($list) -
        //       NOT from  the $letters array.
        $key = $list->key();
        // Note: $value is a stdClass created above.
        //       We actually do not know the property (class->{property})
        //       to access the letter.
        $object = $list->current();

        // Fix to get the property.
        $objectProperty = key(get_object_vars($object));
        // /Fix

        // Get the letter from the object.
        $letter = $object->{$objectProperty};

        echo "{$key} => {$letter}\r\n";
    }
}

/**
 * @param SplObjectStorage $list
 */
function sortList(\SplObjectStorage $list)
{
    $objects = [];
    for (
        $list->rewind(), $i = 0;
        $i < $list->count();
        $i++, $list->next()
    ) {
        $objects[] = $list->current();
    }

    $list->removeAll($list);

    uasort($objects, function (stdClass $a, stdClass $b) {
        // Fix to get the property.
        $objectProperty = key(get_object_vars($a));
        // /Fix
        $aLetter = $a->{$objectProperty};

        // Fix to get the property.
        $objectProperty = key(get_object_vars($b));
        // /Fix
        $bLetter = $b->{$objectProperty};

        if ($aLetter == $bLetter) {
            return 0;
        }
        // a < b === asc ; a > b === desc
        return ($aLetter < $bLetter) ? -1 : 1;
    });

    foreach ($objects as $object) {
        $list->attach($object);
    }
}

printList($list);
// 0 => b
// 1 => a
// 2 => c
// 3 => e
// 4 => f
// 5 => d
echo "------\r\n";

sortList($list);

printList($list);
// 0 => a
// 1 => b
// 2 => c
// 3 => d
// 4 => e
// 5 => f
echo "------\r\n";

这里是 ->letter 解决方案(无评论)。

$letters = [0 => "b", 10 => "a", 22 => "c", 3 => "e", 44 => "f", "d"];
$list = new SplObjectStorage();
foreach ($letters as $key => $value) {
    $o = new stdClass();
    $o->letter = $value;
    $list->attach($o);
}
/**
 * @param SplObjectStorage $list
 */
function printList(\SplObjectStorage $list)
{
    for (
        $list->rewind(), $i = 0;
        $i < $list->count();
        $i++, $list->next()
    ) {
        echo "{$list->key()} => {$list->current()->letter}\r\n";
    }
}

/**
 * @param SplObjectStorage $list
 */
function sortList(\SplObjectStorage $list)
{
    $objects = [];
    for (
        $list->rewind(), $i = 0;
        $i < $list->count();
        $i++, $list->next()
    ) {
        $objects[] = $list->current();
    }
    $list->removeAll($list);
    uasort($objects, function (stdClass $a, stdClass $b) {
        if ($a->letter == $b->letter) {
            return 0;
        }
        // a < b === asc ; a > b === desc
        return ($a->letter < $b->letter) ? -1 : 1;
    });
    foreach ($objects as $object) {
        $list->attach($object);
    }
}

printList($list);
// 0 => b
// 1 => a
// 2 => c
// 3 => e
// 4 => f
// 5 => d
echo "------\r\n";

sortList($list);

printList($list);
// 0 => a
// 1 => b
// 2 => c
// 3 => d
// 4 => e
// 5 => f
echo "------\r\n";