Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. Sorted list

Sorted list

Scheduled Pinned Locked Moved .NET (Core and Framework)
questioncsharp
13 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Pete OHanlon

    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.

    My blog | My articles

    K Offline
    K Offline
    KeesVer
    wrote on last edited by
    #3

    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

    D 1 Reply Last reply
    0
    • K KeesVer

      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

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #4

      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

      K 1 Reply Last reply
      0
      • D Dave Kreskowiak

        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

        K Offline
        K Offline
        KeesVer
        wrote on last edited by
        #5

        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

        P 1 Reply Last reply
        0
        • K KeesVer

          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

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #6

          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.

          My blog | My articles

          K 1 Reply Last reply
          0
          • P Pete OHanlon

            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.

            My blog | My articles

            K Offline
            K Offline
            KeesVer
            wrote on last edited by
            #7

            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

            B K 2 Replies Last reply
            0
            • K KeesVer

              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

              B Offline
              B Offline
              Brady Kelly
              wrote on last edited by
              #8

              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.

              1 Reply Last reply
              0
              • K KeesVer

                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

                K Offline
                K Offline
                Kevin McFarlane
                wrote on last edited by
                #9

                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

                1 Reply Last reply
                0
                • K KeesVer

                  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

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #10

                  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.


                  G 1 Reply Last reply
                  0
                  • L Luc Pattyn

                    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.


                    G Offline
                    G Offline
                    Guffa
                    wrote on last edited by
                    #11

                    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.

                    L 1 Reply Last reply
                    0
                    • G Guffa

                      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.

                      L Offline
                      L Offline
                      Luc Pattyn
                      wrote on last edited by
                      #12

                      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.


                      G 1 Reply Last reply
                      0
                      • L Luc Pattyn

                        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.


                        G Offline
                        G Offline
                        Guffa
                        wrote on last edited by
                        #13

                        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.

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups