Comments on: Sorting PL/SQL Collections, the hard way, the intermediate way and the quite simple way (part one) https://technology.amis.nl/2006/05/31/sorting-plsql-collections-the-hard-way-the-intermediate-way-and-the-quite-simple-way-part-one/ Friends of Oracle and Java Wed, 24 Jun 2015 09:59:44 +0000 hourly 1 http://wordpress.org/?v=4.2.2 By: Michael https://technology.amis.nl/2006/05/31/sorting-plsql-collections-the-hard-way-the-intermediate-way-and-the-quite-simple-way-part-one/#comment-3354 Thu, 20 Sep 2007 20:35:22 +0000 http://technology.amis.nl/blog/?p=1214#comment-3354 Hi,

Thank you for your code. Can you also advice how to sort in desc order?

Thank you again.
Michael

]]>
By: Vizith https://technology.amis.nl/2006/05/31/sorting-plsql-collections-the-hard-way-the-intermediate-way-and-the-quite-simple-way-part-one/#comment-3353 Mon, 26 Feb 2007 05:58:11 +0000 http://technology.amis.nl/blog/?p=1214#comment-3353 i guyz i tried to develop a number puzzle forms6i .. and am in a half way strucked with logic in it .. so can any one help me out to finish my project.

i added the followin code

declare

temp varchar2(40);
lbl varchar2(40);
begin
lbl:=get_item_property(name_in(‘system.trigger_item’),label);
temp:= :system.trigger_item;
if get_item_property(temp,VISIBLE)= ‘TRUE’ then
set_item_property(:parameter.HIDENUM,VISIBLE,PROPERTY_TRUE);
set_item_property(:parameter.HIDENUM,ENABLED,PROPERTY_TRUE);
set_item_property(:parameter.HIDENUM,label,lbl);
go_item(:parameter.HIDENUM);
set_item_property(temp,VISIBLE,PROPERTY_FALSE);
:parameter.HIDENUM:=temp;
end if;
/*if get_item_property(‘number_game.TWO’,displayed)=’TRUE’ then
set_item_property(‘number_game.TWO’,displayed,’FALSE’);
end if;
if get_item_property(‘number_game.THREE’,displayed)=’TRUE’ then
set_item_property(‘number_game.THREE’,displayed,’FALSE’);
end if;*/
if lbl =’EXIT’ then exit_form(no_validate);
end if;
end;

]]>
By: Miquel https://technology.amis.nl/2006/05/31/sorting-plsql-collections-the-hard-way-the-intermediate-way-and-the-quite-simple-way-part-one/#comment-3352 Wed, 01 Nov 2006 21:59:00 +0000 http://technology.amis.nl/blog/?p=1214#comment-3352 With a little adjustement (2 dimension table for sorting) it works for duplicates:
/* Formatted on 2006/11/01 20:11 (Formatter Plus v4.8.5) */
DECLARE
— define and initialize a simple Nested Table Collection
TYPE num_tbl IS TABLE OF NUMBER;

l_num_tbl num_tbl := num_tbl (6, 12, 9, 1, 54, 21, 11, 2, 12, 12, 11);
l_idx INTEGER;
i INTEGER;
BEGIN
DECLARE
— here is where the sorting magic starts
— we create an Associative Array that is indexed by binary_integer
— we know that this collections keys (the only thing we care about in this case)
— will always be kept in sorted order
TYPE num_aat_t IS TABLE OF NUMBER
INDEX BY BINARY_INTEGER;

TYPE num_aat_t2 IS TABLE OF num_aat_t
INDEX BY BINARY_INTEGER;
— two dimension table to handle duplicates
l_num_aat num_aat_t2;
BEGIN
l_idx := l_num_tbl.FIRST;

— loop over all elements in the l_num_tbl collection
— that we want to sort. Use every element in l_num_tbl
— as a key for the l_num_aat associative array. Associate
— the key with a meaningless value; we do not care about
— the value in this case.
LOOP
i := 1;

— if duplicate increment second dimension
WHILE l_num_aat.EXISTS (l_num_tbl (l_idx))
AND l_num_aat (l_num_tbl (l_idx)).EXISTS (i)
LOOP
i := i + 1;
END LOOP;

l_num_aat (l_num_tbl (l_idx)) (i) := 0;
l_idx := l_num_tbl.NEXT (l_idx);
EXIT WHEN l_idx IS NULL;
END LOOP;

— remove all elements from l_num_tbl
l_num_tbl.DELETE;
— start repopulating l_num_tbl – in the proper order –
— from the sorted collection of keys in the l_num_aat collection
l_idx := l_num_aat.FIRST;

LOOP
FOR i IN 1 .. l_num_aat (l_idx).LAST
LOOP
l_num_tbl.EXTEND;
l_num_tbl (l_num_tbl.LAST) := l_idx;
END LOOP;

l_idx := l_num_aat.NEXT (l_idx);
EXIT WHEN l_idx IS NULL;
END LOOP;
END;

— DONE! At this point, l_num_tbl is properly sorted
l_idx := l_num_tbl.FIRST;

LOOP
DBMS_OUTPUT.put_line (‘ ** ‘ || l_num_tbl (l_idx));
l_idx := l_num_tbl.NEXT (l_idx);
EXIT WHEN l_idx IS NULL;
END LOOP;
END;

]]>
By: Ziya Åženol https://technology.amis.nl/2006/05/31/sorting-plsql-collections-the-hard-way-the-intermediate-way-and-the-quite-simple-way-part-one/#comment-3351 Thu, 03 Aug 2006 15:28:58 +0000 http://technology.amis.nl/blog/?p=1214#comment-3351 Thanks for this example

]]>
By: firefight https://technology.amis.nl/2006/05/31/sorting-plsql-collections-the-hard-way-the-intermediate-way-and-the-quite-simple-way-part-one/#comment-3350 Sun, 09 Jul 2006 20:56:03 +0000 http://technology.amis.nl/blog/?p=1214#comment-3350 The source of the problem is that PL/SQL is severly lacking in many areas, e.g. there’s no language support for casting the record type to a generic type, and implementing a “Comparable” interface, al la Java & Collections.sort.

It being designed after Pascal led to so many of the problems with type system.

]]>
By: Aino https://technology.amis.nl/2006/05/31/sorting-plsql-collections-the-hard-way-the-intermediate-way-and-the-quite-simple-way-part-one/#comment-3349 Wed, 31 May 2006 12:58:15 +0000 http://technology.amis.nl/blog/?p=1214#comment-3349 Another pitfall of this approach is that duplicates will disappear from the collection and will probably cause some unexpected behaviour with ‘reference swapping’.

]]>
By: Lucas Jellema https://technology.amis.nl/2006/05/31/sorting-plsql-collections-the-hard-way-the-intermediate-way-and-the-quite-simple-way-part-one/#comment-3348 Wed, 31 May 2006 10:31:39 +0000 http://technology.amis.nl/blog/?p=1214#comment-3348 Gary,

I can understand your hesitation. I think the approach outlined in the next installment on Sorting PL/SQL Collections may appeal more to you. See Sorting PL/SQL Collections, the quite simple way (part two: Have the SQL Engine do the heavy lifting)

]]>
By: Francois https://technology.amis.nl/2006/05/31/sorting-plsql-collections-the-hard-way-the-intermediate-way-and-the-quite-simple-way-part-one/#comment-3347 Wed, 31 May 2006 06:42:28 +0000 http://technology.amis.nl/blog/?p=1214#comment-3347 Brilliant, as allways. I’m waiting for the part 2 ;o)

]]>
By: Gary https://technology.amis.nl/2006/05/31/sorting-plsql-collections-the-hard-way-the-intermediate-way-and-the-quite-simple-way-part-one/#comment-3346 Wed, 31 May 2006 01:15:29 +0000 http://technology.amis.nl/blog/?p=1214#comment-3346 “alphabetical order ”
This is the bit that has worried me. I haven’t found a document reference to the actual ordering sequence. I’ve assumed that it basic ASCII, so you’d get ‘a’ sorted after ‘A’ and ‘B’. I haven’t tested against a machine that is natively EBCDIC, which has ‘a’ before ‘A’ and ‘B’.
And then there’s multilingual sorts….
Just something to bear in mind (and test) if appropriate to your situation.

]]>