Это должно быть то, что вы ищете. Обратите внимание, что лучше вводить переменные / коллекции из таблиц, как в вашем примере. Я просто использовал универсальные версии, так как у меня нет ваших таблиц для работы. Если вы не понимаете, как это работает, не стесняйтесь спрашивать. Я предполагаю, что это домашнее задание (кто еще будет разбираться в Oracle), поэтому цель задания - понять это, а не просто сделать это правильно. :)
DECLARE
coll DBMS_SQL.VARCHAR2A;
swapped BOOLEAN;
tmp VARCHAR2(10);
BEGIN
/*
Generate 10 random strings and collect them into our collection
Note: you would replace this with your query on marshall.team
*/
select dbms_random.string('l',10) rand_string
BULK COLLECT INTO coll
from dual
connect by level <= 10;
/*
At this point, all of the rows we need are in our collection
so there is no need to go back to the table anymore. Now onto the...
Bubble sort.. walk through the collection swapping elements until
we make a pass where no swapping takes place
*/
LOOP
swapped := false;
FOR i IN 2 .. coll.LAST
LOOP
IF coll(i-1) > coll(i)
THEN
-- swap records
tmp := coll(i);
coll(i) := coll(i-1);
coll(i-1) := tmp;
/*
Mark that swap has taken place. note we mark as true only inside
the if block, meaning a swap really did take place
*/
swapped := true;
END IF;
END LOOP;
-- If we passed through table without swapping we are done, so exit
EXIT WHEN NOT swapped;
END LOOP;
/*
Now print out records to make sure they are in order. Again notice
how we are just referencing the (now sorted) collection and not going
back to the table again
*/
FOR i in coll.FIRST .. coll.LAST
LOOP
dbms_output.put_line(coll(i));
END LOOP;
END;
/