I have an array of letters that must be alphabetically sorted in a SplObjectStorage()
set
using SPL methods such as rewind()
, current()
, next()
, and valid()
. Currently, my while
loop runs indefinitely without sorting anything, but I'm not sure why. I haven't seen anything online related to sorting either SPL doubly linked lists or Object Storage's so hopefully this will be useful to 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"]
?>
I do not think you can sort the objects in the storage object - at least i did not find a quick fix and i think it would be way to expensive to loop in a while ... .
So here the easy solution: get all off the storage, sort them, set them back.
Note: i added some comments to let you know what were wrong ect. If you are in the position to create the storage then i would suggest to use "attribute" or "letter" or w/e as property, and not the current numeric key from the letters array.
Note: i added a solution with the property ->letter
at the bottom.
// 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";
And here the ->letter
solution (without comments).
$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";