Justin R. Erenkrantz Where do you want to go today?

This file shows how it easy to sort an array with Eiffel. It has a built-in Collection, Collection_Sorter, and Reverse_Collection_Sorter classes that handle all sorting functions for the type. Like Ada, you must explicitly declare the collection to contain a specific type. Notice the <<Items>> construct for declaring constant arrays. One can also see Eiffel's support for forward declarations by using print_collection before its formal definition. Within the print_collection function, there is the explicit type definition in print_collection(c: COLLECTION[STRING]) which ensures that the collection only contains strings.

-- This file is  free  software, which  comes  along  with  SmallEiffel. This
-- software  is  distributed  in the hope that it will be useful, but WITHOUT
-- ANY  WARRANTY;  without  even  the  implied warranty of MERCHANTABILITY or
-- FITNESS  FOR A PARTICULAR PURPOSE. You can modify it as you want, provided
-- this header is kept unaltered, and a notification of the changes is added.
-- You  are  allowed  to  redistribute  it and sell it, alone or as a part of
-- another product.
--          Copyright (C) 1994-98 LORIA - UHP - CRIN - INRIA - FRANCE
--            Dominique COLNET and Suzanne COLLIN - colnet@loria.fr
--                       http://SmallEiffel.loria.fr
--
class DEMO2
   --
   -- Yes it is easy to sort any COLLECTION :
   -- 
   --            compile -o demo2 -boost demo2
   --

creation make

feature

   make is
      local
         c: COLLECTION[STRING];
         sorter: COLLECTION_SORTER[STRING];
         reverse_sorter: REVERSE_COLLECTION_SORTER[STRING];
      do
         c := <<"dd", "bb", "aa", "cc">>;
         io.put_string("My collection not sorted : ");
         print_collection(c);
         io.put_string("My sorted collection     : ");
         sorter.sort(c);
         print_collection(c);
         io.put_string("Reverse sorting          : ");
         reverse_sorter.sort(c);
         print_collection(c);
      end;

feature {NONE}

   print_collection(c: COLLECTION[STRING]) is
      local
         i: INTEGER;
      do
         from
            i := c.lower;
         until
            i > c.upper
         loop
            io.put_string(c.item(i));
            io.put_character(' ');
            i := i + 1;
         end;
         io.put_character('%N');
      end;
   

end -- DEMO2

Last Modified Sunday, 05-Aug-2018 12:51:45 EDT These pages were made by Justin R. Erenkrantz unless otherwise stated. This work is licensed under a Creative Commons License. These pages will look best in an XHTML 1.0 compliant browser.

Creative Commons Attribution License Valid XHTML 1.0 Strict! Valid CSS!
[Blue Ribbon Campaign icon] [frdm] Support SFLC