Skip to Content
C# 3.0 Cookbook, 3rd Edition
book

C# 3.0 Cookbook, 3rd Edition

by Jay Hilyard, Stephen Teilhet
December 2007
Intermediate to advanced
896 pages
19h 57m
English
O'Reilly Media, Inc.
Content preview from C# 3.0 Cookbook, 3rd Edition

4.7. Reversing the Contents of a Sorted List

Problem

You want to be able to reverse the contents of a sorted list of items while also maintaining the ability to access them in both array and list styles like SortedList and the generic SortedList<T> classes provide. Neither SortedList nor SortedList<T> provides a direct way to accomplish this without reloading the list.

Solution

Use LINQ to Objects to query the SortedList<T> and apply a descending order to the information in the list. After instantiating a SortedList<TKey, TValue>, the key of which is an int and the value of which is a string, a series of unordered numbers and their text representations are inserted into the list. Those items are then displayed:

	SortedList<int, string> data = new SortedList<int, string>(); 
	data.Add(2, "two"); 
	data.Add(5, "five"); 
	data.Add(3, "three"); 
	data.Add(1, "one");

	foreach (KeyValuePair<int, string> kvp in data) 
	{ 

	    Debug.WriteLine("\t" + kvp.Key + "\t" + kvp.Value); 
	}

The output for the list is shown sorted in ascending order (the default):

	1    one 
	2    two 
	3    three 
	5    five

Now the sort order is reversed by creating a query using LINQ to Objects and setting the orderby clause to descending. The results are then displayed from the query result set:

	// query ordering by descending
	var query = from d in data 
	            orderby d.Key descending 
	            select d;

	foreach (KeyValuePair<int, string> kvp in query) 
	{ 
	    Debug.WriteLine("\t" + kvp.Key + "\t" + kvp.Value); 
	}

This time the output is in descending order:

 5 five 3 three 2 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

C# 6.0 Cookbook, 4th Edition

C# 6.0 Cookbook, 4th Edition

Stephen Teilhet, Jay Hilyard
C# Cookbook

C# Cookbook

Joe Mayo
C# Cookbook

C# Cookbook

Stephen Teilhet, Jay Hilyard

Publisher Resources

ISBN: 9780596516109Errata Page