Collections para ordenar chaves e valores

 

SortedList  namespace

System.Collections

 

Declaration

SortedList = class(System.Object, IDictionary, ICollection,

  IEnumerable, ICloneable);

 

Representa uma coleção de pares de chave-e-valor que são ordenados pelas chaves e são acessível através de chave e através de índice.

 

program System_Collections_SortedList;

{$APPTYPE CONSOLE}

{$AUTOBOX ON}

 

uses

  System.Collections;

 

type

 SamplesSortedList = class

 public

  class procedure Main;

 private

  class procedure PrintKeysAndValues(myList: SortedList);

 end;

 

{ SamplesSortedList }

 

class procedure SamplesSortedList.Main;

var

  mySL: SortedList;

begin

  // Creates and initializes a new SortedList

  mySL := SortedList.Create;

  mySL.Add('First', 'Hello');

  mySL.Add('Second', 'World');

  mySL.Add('Third', '!');

 

  // Displays the properties

  //and values of the SortedList

  Console.WriteLine('mySL');

  Console.WriteLine('  Count:    {0}', mySL.Count);

  Console.WriteLine('  Capacity: {0}', mySL.Capacity);

  Console.WriteLine('  Keys and Values:');

 

  PrintKeysAndValues(mySL);

end;

 

class procedure SamplesSortedList.PrintKeysAndValues (

  myList: SortedList);

var

  idx: Integer;

begin

  Console.WriteLine(''#9'-KEY-'#9'-VALUE-');

  idx := 0;

  while (idx < myList.Count) do

  begin

    Console.WriteLine(''#9'{0}:'#9'{1}',

      myList.GetKey(idx),

      myList.GetByIndex(idx));

    idx := idx + 1;

  end;

  Console.WriteLine;

end;

 

//test program

begin

  SamplesSortedList.Main;

end.

 

Saída

mySL

  Count:    3

  Capacity: 16

  Keys and  Values:

  -KEY-                        -VALUE-

  First:                          Hello

  Second:                      World

  Third:                         !