Sorted list
-
Pretty much any collection in .NET can be sorted with the aid of the IComparable interface.
Deja View - the feeling that you've seen this post before.
-
Pete, I want the sort order to be maintained so that new items are inserted at the right position in the list without calling List<>.Sort every time. Kees
Well, you wouldn't do this since the more items you add to the collection, the longer the process of adding a single item would take. Sorting is a presentation layer requirement, not something you normally do as part of data storage. If you wanted the list sorted, you would normally call a method on the collection to return the sorted list.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007 -
Well, you wouldn't do this since the more items you add to the collection, the longer the process of adding a single item would take. Sorting is a presentation layer requirement, not something you normally do as part of data storage. If you wanted the list sorted, you would normally call a method on the collection to return the sorted list.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007Thanks for your answer. I have many occasions in which I use sorted lists! For example to prevent insertion of duplicate items and for fast retrieval of existing items. For example, in my situation I have a function parser which can handle various predefined functions. These functions are simply strings and I want to test if a certain string is a predefined function. Looking up a string in a sorted list would be much faster than doing so from an unsorted list. Kees
-
Thanks for your answer. I have many occasions in which I use sorted lists! For example to prevent insertion of duplicate items and for fast retrieval of existing items. For example, in my situation I have a function parser which can handle various predefined functions. These functions are simply strings and I want to test if a certain string is a predefined function. Looking up a string in a sorted list would be much faster than doing so from an unsorted list. Kees
KeesVer wrote:
prevent insertion of duplicate items and for fast retrieval of existing items
Don't use a sorted list for this. This is what a key/value paired collection or is for, such as a Dictionary.
Deja View - the feeling that you've seen this post before.
-
KeesVer wrote:
prevent insertion of duplicate items and for fast retrieval of existing items
Don't use a sorted list for this. This is what a key/value paired collection or is for, such as a Dictionary.
Deja View - the feeling that you've seen this post before.
-
Thanks, I do not always have a key AND a value. Sometimes the Key IS the Value and therefore using a Dictionary or SortedList is cumbersome. However, from your answers I conclude that under .Net this is the way to go. Kees
I would then suggest composing a new class that contains a Dictionary, but adds overloads to methods such as Add, in which you use the value as a key.
-
Thanks, I do not always have a key AND a value. Sometimes the Key IS the Value and therefore using a Dictionary or SortedList is cumbersome. However, from your answers I conclude that under .Net this is the way to go. Kees
If you find the .NET collections not convenient or flexible enough for your tastes be aware that there are some well-tested alternative open source collection libraries around, e.g., PowerCollections[^] Welcome to Power Collections, A Community Project to Develop the Best Public License type safe Collection Classes for .NET. Power Collections makes heavy use of .NET Generics. The goal of the project is to provide generic collection classes that are not available in the .NET framework. Some of the collections included are the Deque, MultiDictionary, Bag, OrderedBag, OrderedDictionary, Set, OrderedSet, and OrderedMultiDictionary. and The C5 Generic Collection Library[^] C5 is a library of generic collection classes for C# and other CLI languages and works with Microsoft .Net version 2.0 and Mono version 1.2 and later. C5 provides functionality and data structures not provided by the standard .Net System.Collections.Generic namespace, such as persistent tree data structures, heap based priority queues, hash indexed array lists and linked lists, and events on collection changes. Also, it is more comprehensive than collection class libraries on other similar platforms, such as Java. Unlike many other collection class libraries, C5 is designed with a strict policy of supporting "code to interface not implementation". I myself have been using PowerCollections in the past few days in connection with user role management. PowerCollections is a little easier to use as it's a natural extension of the framework classes. C5 is more powerful but rather more esoteric. Also PowerCollections is better documented with intellisense tooltips and so on. Both are accompanied by unit tests.
Kevin
-
Hello, How can I create a sorted list in .Net? Something like: SortedList L = new SortedList(); L.Add("B"); L.Add("A"); Now L[0] should contain "A". I found class SortedList but this requires two types which seems an overkill to me. Thanks in advance, Kees Vermeulen
Hi Kees, 1. SortedList is a HashTable or Dictionary, so it stores (Key,Value) pairs. If you don't need values, you could specify "null" for all of them. 2. Yes, I found the collection names a bit confusing; I would expect a Dictionary to always be sorted (as in: Van Dale) but that is not how .NET sees it. 3. Maybe this will interest you: http://www.itu.dk/research/c5/[^] :)
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.
-
Hi Kees, 1. SortedList is a HashTable or Dictionary, so it stores (Key,Value) pairs. If you don't need values, you could specify "null" for all of them. 2. Yes, I found the collection names a bit confusing; I would expect a Dictionary to always be sorted (as in: Van Dale) but that is not how .NET sees it. 3. Maybe this will interest you: http://www.itu.dk/research/c5/[^] :)
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.
Luc Pattyn wrote:
I found the collection names a bit confusing; I would expect a Dictionary to always be sorted
Well, that's probably just because real world dictionaries are sorted in order for us to find anything in them. If you look at how a paper dictionary works, a sorted list would be digital equivalent, but if you instead concentrate on the purpose of a dictionary, i.e. to find single items in a large collection, the naming of the class makes more sense. :)
Experience is the sum of all the mistakes you have done.
-
Luc Pattyn wrote:
I found the collection names a bit confusing; I would expect a Dictionary to always be sorted
Well, that's probably just because real world dictionaries are sorted in order for us to find anything in them. If you look at how a paper dictionary works, a sorted list would be digital equivalent, but if you instead concentrate on the purpose of a dictionary, i.e. to find single items in a large collection, the naming of the class makes more sense. :)
Experience is the sum of all the mistakes you have done.
Yes, I know what they do, but why choose names that are counterintuitive, i.e. don't match real-world terms. I would prefer a Map for a .NET Dictionary, and a SortedMap for a .NET SortedList (it isn't a sorted List, in .NET terms it is a sorted dictionary). :)
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.
-
Yes, I know what they do, but why choose names that are counterintuitive, i.e. don't match real-world terms. I would prefer a Map for a .NET Dictionary, and a SortedMap for a .NET SortedList (it isn't a sorted List, in .NET terms it is a sorted dictionary). :)
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.
Luc Pattyn wrote:
Yes, I know what they do, but why choose names that are counterintuitive, i.e. don't match real-world terms.
They match the real-world items if you think of them from a certain point of view. Computerised real-world concepts often mimic only some aspects but not all. If you take radio buttons for example, they pop out just like buttons on a real radio, but you can't turn them to change the preset frequency.
Luc Pattyn wrote:
I would prefer a Map for a .NET Dictionary
A map to me is something that describes two dimensional geographical data, so that would be even more confusing...
Luc Pattyn wrote:
and a SortedMap for a .NET SortedList (it isn't a sorted List, in .NET terms it is a sorted dictionary).
The internal storage for a SortedList is a pair of arrays, so it actually is a sorted list. :)
Despite everything, the person most likely to be fooling you next is yourself.