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. C#
  4. Sorted Collection in C#

Sorted Collection in C#

Scheduled Pinned Locked Moved C#
csharpalgorithmsdata-structures
9 Posts 8 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.
  • U Offline
    U Offline
    User 10309042
    wrote on last edited by
    #1

    I need a collection, which is easy to sort (or sorted collection). If it is almost sorted, the sorting time must be minimal. Or it can be collection, which keeps its elements sorted, even if you change the keys of elements. The keys in collection must NOT be unique! (Maybe some sort of tree collection internally.)

    L B N A D 7 Replies Last reply
    0
    • U User 10309042

      I need a collection, which is easy to sort (or sorted collection). If it is almost sorted, the sorting time must be minimal. Or it can be collection, which keeps its elements sorted, even if you change the keys of elements. The keys in collection must NOT be unique! (Maybe some sort of tree collection internally.)

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Some to choose from[^].

      Veni, vidi, abiit domum

      M 1 Reply Last reply
      0
      • L Lost User

        Some to choose from[^].

        Veni, vidi, abiit domum

        M Offline
        M Offline
        Matt T Heffron
        wrote on last edited by
        #3

        All of the standard sorted collections require unique keys which violates one of OP's requirements.

        1 Reply Last reply
        0
        • U User 10309042

          I need a collection, which is easy to sort (or sorted collection). If it is almost sorted, the sorting time must be minimal. Or it can be collection, which keeps its elements sorted, even if you change the keys of elements. The keys in collection must NOT be unique! (Maybe some sort of tree collection internally.)

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Member 10339829 wrote:

          If it is almost sorted, the sorting time must be minimal.

          We always strive for minimal time; how many elements are in your list?

          Member 10339829 wrote:

          The keys in collection must NOT be unique!

          Then they're no longer "keys", but simply values. That equates to an array of objects - so, an IComparer would sort that.

          Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

          1 Reply Last reply
          0
          • U User 10309042

            I need a collection, which is easy to sort (or sorted collection). If it is almost sorted, the sorting time must be minimal. Or it can be collection, which keeps its elements sorted, even if you change the keys of elements. The keys in collection must NOT be unique! (Maybe some sort of tree collection internally.)

            B Offline
            B Offline
            BillWoodruff
            wrote on last edited by
            #5

            You need a collection of ... what ? And, what is the "sorting algorithm" you require/need/want ? .NET hands you the ability to easily sort (using a QuickSort algorithm) generic Lists of value types by just invoking the 'Sort function: "All numeric types (such as Int32 and Double) implement IComparable, as do String, Char, and DateTime. Custom types should also provide their own implementation of IComparable to enable object instances to be ordered or sorted." And, writing your own IComparable is not that difficult; for example: If you have a TreeNode Collection:

            public List<TreeNode> sTNList = new List<TreeNode>
            {
            new TreeNode("1"),
            new TreeNode("1"),
            new TreeNode("2"),
            new TreeNode("4"),
            new TreeNode("1"),
            new TreeNode("4"),
            new TreeNode("8"),
            new TreeNode("2"),
            new TreeNode("3")
            };

            You can easily define a custom IComparer Class to sort the nodes:

            public class TreeNodeICompare : IComparer<TreeNode>
            {
            public int Compare(TreeNode x, TreeNode y)
            {
            // basic alphanumeric sort
            return(x.Text.CompareTo(y.Text));
            }
            }

            And invoke it:

            sTNList.Sort(0, sTNList.Count, new TreeNodeICompare());

            See [^] for the documentation on the call to 'Sort used here. And, note: "This method uses Array.Sort, which uses the QuickSort algorithm. This implementation performs an unstable sort; that is, if two elements are equal, their order might not be preserved. In contrast, a stable sort preserves the order of elements that are equal. ... On average, this method is an O(n log n) operation, where n is Count; in the worst case it is an O(n ^ 2) operation." Using a non-unique property of a collection, like the 'Text property of a TreeNode, you avoid possible duplicate key issues that might arise with other types of collections. Perhaps if you describe the scenario in which your collection is being used you can get more than a "mind-reader's guess" type of response here. bill

            Google CEO, Erich Schmidt: "I keep asking for a product called Serendipity. This product would have access to everything ever written or recorded, know everything the user ever worked on and saved to his or her personal hard drive, and know a whole lot about the user's tastes, friends and predilections." 2004, USA Today interview

            1 Reply Last reply
            0
            • U User 10309042

              I need a collection, which is easy to sort (or sorted collection). If it is almost sorted, the sorting time must be minimal. Or it can be collection, which keeps its elements sorted, even if you change the keys of elements. The keys in collection must NOT be unique! (Maybe some sort of tree collection internally.)

              N Offline
              N Offline
              NWPU_Tbeck
              wrote on last edited by
              #6

              what the sort rule? how about the SortedDictionary? http://msdn.microsoft.com/en-us/library/f7fta44c%28v=vs.85%29.aspx[^]

              1 Reply Last reply
              0
              • U User 10309042

                I need a collection, which is easy to sort (or sorted collection). If it is almost sorted, the sorting time must be minimal. Or it can be collection, which keeps its elements sorted, even if you change the keys of elements. The keys in collection must NOT be unique! (Maybe some sort of tree collection internally.)

                A Offline
                A Offline
                Abhinav S
                wrote on last edited by
                #7

                If your list is almost sorted, you could use your own sort alogrithm. If your run some tests[^], you will notice bubble sort does fairly well with lists that are almost sorted.

                Apps - Color Analyzer | Arctic | XKCD | Sound Meter | Speed Dial

                1 Reply Last reply
                0
                • U User 10309042

                  I need a collection, which is easy to sort (or sorted collection). If it is almost sorted, the sorting time must be minimal. Or it can be collection, which keeps its elements sorted, even if you change the keys of elements. The keys in collection must NOT be unique! (Maybe some sort of tree collection internally.)

                  D Offline
                  D Offline
                  Dinkar Tiwari
                  wrote on last edited by
                  #8

                  you can getbetter solution by below link Sorting in Collection[^] :thumbsup:

                  1 Reply Last reply
                  0
                  • U User 10309042

                    I need a collection, which is easy to sort (or sorted collection). If it is almost sorted, the sorting time must be minimal. Or it can be collection, which keeps its elements sorted, even if you change the keys of elements. The keys in collection must NOT be unique! (Maybe some sort of tree collection internally.)

                    G Offline
                    G Offline
                    Gabriel Szabo
                    wrote on last edited by
                    #9

                    You could use the OrderBy<TSource, TKey>(Func<TSource, TKey>) extension method. The actual sorting is deffered until result gets enumerated, so the call to OrderBy() itself is fast. It performs a stable sort, which means that the order of already sorted elements is preserved. it does not sort the original sequence in-place. Instead, the returned IEnumerable returns elements of the original sequence in sorted order. And you can speed things up a bit with parallel LINQ like:

                    var result = sourceSequence.AsParallel().OrderBy(item => item.Key);

                    Gabriel Szabo

                    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