Test in a live environment
Test in production without watermarks.
Works wherever you need it to.
The C# Sorted List class in C# is a collection of key-value pairs, similar to a dictionary, but with the added benefit of automatic sorting by keys. It’s part of the System.Collections.Generic namespace and is designed for scenarios where you need quick access to sorted data. SortedList TKey, TValue is ideal when you need to maintain data in a particular order and access elements by key efficiently.
When it comes to working with SortedLists alongside PDF generation tasks, IronPDF integrates perfectly with this class, giving you even more control over PDF generation.
The public class SortedList is a hybrid between an array and a hash table, and it organizes its items by key. Internally, it uses a sorted array to keep key values in order, meaning that while lookups by key are efficient, insertion and deletion operations can be slower than in a Dictionary.
By default, SortedList TKey, TValue sorts keys in ascending order using the IComparable interface, which ensures that string keys and other types implement a default comparison behavior. If a custom sorting order is needed, a custom comparer can be provided.
Pros:
Cons:
To create a SortedList in C#, you can use either the default constructor or pass in an IComparer if you need custom sorting. The SortedList has a default initial capacity of 16, but if you know the approximate size beforehand, this can be specified to improve performance.
SortedList<int, string> sortedList = new SortedList<int, string>();
SortedList<int, string> sortedList = new SortedList<int, string>();
Dim sortedList As New SortedList(Of Integer, String)()
You can add key-value pairs to the SortedList using the Add method, which will keep items sorted by key. In this following code example, the SortedList is sorted by the specified key values, automatically keeping the data in ascending order.
sortedList.Add(1, "Apple");
sortedList.Add(3, "Banana");
sortedList.Add(2, "Cherry");
sortedList.Add(1, "Apple");
sortedList.Add(3, "Banana");
sortedList.Add(2, "Cherry");
sortedList.Add(1, "Apple")
sortedList.Add(3, "Banana")
sortedList.Add(2, "Cherry")
Access elements in a SortedList by using their keys. You can retrieve or modify any specified value associated with keys directly.
// Accessing a specific value by key
string value = sortedList[1]; // "Apple"
// Modifying a value
sortedList[1] = "Avocado";
// Accessing a specific value by key
string value = sortedList[1]; // "Apple"
// Modifying a value
sortedList[1] = "Avocado";
' Accessing a specific value by key
Dim value As String = sortedList(1) ' "Apple"
' Modifying a value
sortedList(1) = "Avocado"
You can remove items using specified keys using the Remove method or remove them using the specified index using RemoveAt. Both options allow for controlled deletion of specified object data in the SortedList.
sortedList.Remove(3); // Removes key 3
sortedList.RemoveAt(0); // Removes item at a zero-based index 0
sortedList.Remove(3); // Removes key 3
sortedList.RemoveAt(0); // Removes item at a zero-based index 0
sortedList.Remove(3) ' Removes key 3
sortedList.RemoveAt(0) ' Removes item at a zero-based index 0
You can iterate over SortedList using a foreach loop, which retrieves both keys and values in sorted order.
foreach (KeyValuePair<int, string> kvp in sortedList)
{
Console.WriteLine($"Key: {kvp.Key}, Value: {kvp.Value}");
}
foreach (KeyValuePair<int, string> kvp in sortedList)
{
Console.WriteLine($"Key: {kvp.Key}, Value: {kvp.Value}");
}
For Each kvp As KeyValuePair(Of Integer, String) In sortedList
Console.WriteLine($"Key: {kvp.Key}, Value: {kvp.Value}")
Next kvp
Example 2: Using SortedList in more complex scenarios, such as displaying sorted transaction logs or ranked scores. Best Practices:
IronPDF is a powerful library for generating and modifying PDF files in C#. It allows developers to create PDFs from various sources, add content programmatically, and customize PDF layouts. In this section, we’ll use IronPDF to create a PDF report from SortedList data.
To start using IronPDF, install the IronPDF NuGet package:
Install-Package IronPdf
Install-Package IronPdf
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'Install-Package IronPdf
The following example demonstrates how to export data from a SortedList to a PDF table.
First, set up your SortedList:
SortedList<int, string> sortedList = new SortedList<int, string>
{
{ 1, "Apple" },
{ 2, "Banana" },
{ 3, "Cherry" }
};
SortedList<int, string> sortedList = new SortedList<int, string>
{
{ 1, "Apple" },
{ 2, "Banana" },
{ 3, "Cherry" }
};
Dim sortedList As New SortedList(Of Integer, String) From {
{1, "Apple"},
{2, "Banana"},
{3, "Cherry"}
}
Next, use IronPDF to generate a PDF from this data:
ChromePdfRenderer renderer = new ChromePdfRenderer();
string html = "<h1>Sorted List Data</h1><table border='1'><tr><th>Key</th><th>Value</th></tr>";
foreach (var kvp in sortedList)
{
html += $"<tr><td>{kvp.Key}</td><td>{kvp.Value}</td></tr>";
}
html += "</table>";
PdfDocument pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs("sortedList.pdf");
ChromePdfRenderer renderer = new ChromePdfRenderer();
string html = "<h1>Sorted List Data</h1><table border='1'><tr><th>Key</th><th>Value</th></tr>";
foreach (var kvp in sortedList)
{
html += $"<tr><td>{kvp.Key}</td><td>{kvp.Value}</td></tr>";
}
html += "</table>";
PdfDocument pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs("sortedList.pdf");
Dim renderer As New ChromePdfRenderer()
Dim html As String = "<h1>Sorted List Data</h1><table border='1'><tr><th>Key</th><th>Value</th></tr>"
For Each kvp In sortedList
html &= $"<tr><td>{kvp.Key}</td><td>{kvp.Value}</td></tr>"
Next kvp
html &= "</table>"
Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf(html)
pdf.SaveAs("sortedList.pdf")
This code creates an HTML table from the SortedList data and converts it into a PDF using IronPDF.
In this article, we explored the C# SortedList class, a powerful tool for managing sorted, key-value data collections. SortedList is especially useful in scenarios where order and efficient access by key are critical. From creating, adding, and removing elements to integrating with IronPDF for PDF export, we covered practical steps and best practices for using SortedList in real-world applications.
Additionally, we showcased how IronPDF can simplify the task of exporting specified values from a SortedList to PDF format, allowing for the easy creation of professional, well-organized reports. IronPDF’s versatility, including customizable headers, footers, and CSS styling, makes it an excellent choice for generating PDFs directly from your C# applications.
If you’re interested in trying IronPDF, it offers a free trial that allows you to explore its full range of features without commitment. With this trial, you can test PDF generation, customization options, and integration into their existing projects to ensure it meets their needs. By combining SortedList and IronPDF, developers gain a robust, efficient solution for managing and reporting sorted data in C# applications.
9 .NET API products for your office documents