1. Home
  2. Computing & Technology
  3. Delphi Programming
Delphi for .NET FCL Examples Reference
SortedList namespace
System.Collections
Declaration
SortedList = class(System.Object, IDictionary, ICollection, IEnumerable, ICloneable);
Description
Represents a collection of key-and-value pairs that are sorted by the keys and are accessible by key and by index.
Example
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.
Example output
mySL
  Count:    3
  Capacity: 16
  Keys and Values:
    -KEY-    -VALUE-
    First:    Hello
    Second:    World
    Third:    !

Looking for Delphi for .NET examples? Look no further, Delphi for .NET FCL Examples Reference is what you are looking for!

Explore Delphi Programming
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming

©2009 About.com, a part of The New York Times Company.

All rights reserved.